0

before_filter懸念から呼び出されていることをテストしようとしています。私のテストは次のようになります。

class AuthorizableController < ApplicationController
  include Authorizable
end

describe Authorizable do
  let(:dummy) { AuthorizableController.new }

  it "adds a before filter to the class" do
    AuthorizableController.expects(:before_filter).with(:authorize)

    dummy.class_eval do |klass|
      include Authorizable
    end
  end
end

私の懸念は次のようになります。

module Authorizable
  extend ActiveSupport::Concern

  included do
    before_filter :authorize
  end
end

...そして、次のようなエラーが表示されます(RSpecを使用している場合、モカについては言及されておらず、代わりにMiniTest...):

Failures:

  1) Authorizable adds a before filter to the class
     Failure/Error: AuthorizableController.expects(:before_filter).with(:authorize)
     MiniTest::Assertion:
       not all expectations were satisfied
       unsatisfied expectations:
       - expected exactly once, not yet invoked: AuthorizableController.before_filter(:authorize)
     # ./spec/controllers/concerns/authorizable_spec.rb:11:in `block (2 levels) in <top (required)>'
4

1 に答える 1

0

rails メソッドclass_evalは、具体的なクラスではなく、問題のオブジェクトのシングルトン クラスを評価します。シングルトン クラスとは何かについてのこの質問には、さらに詳しい説明があります。フィルターがそのシングルトン クラスに追加されているためauthorize、メイン クラスで呼び出されるという期待は満たされません。

次のシングルトン クラスに期待値を追加できますdummy

dummy.singleton_class.expects(:before_filter).with(:authorize) 
于 2013-07-23T12:03:49.400 に答える