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 时,没有提到 mocha,而是 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 回答