2013-07-23 18 views
0

我試圖測試before_filter是從一個問題被調用。我的測試是這樣的:摩卡嘲諷類的方法包括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 

...和我得到看起來像這樣的錯誤(沒有提及的摩卡咖啡,而是MINITEST,當我使用RSpec的...):

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)>' 

回答

0

軌道方法class_eval評估的單類問題的對象不是具體類的。在this question中有更多關於單身人士課程的解釋。由於過濾器被添加到該單例類,因此您不希望在主類上調用authorize

你可以在單獨的類中添加預期爲dummy

dummy.singleton_class.expects(:before_filter).with(:authorize)