Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Mocha mocking类方法包含在过滤器之前_Ruby On Rails_Rspec_Mocha.js - Fatal编程技术网

Ruby on rails Mocha mocking类方法包含在过滤器之前

Ruby on rails Mocha mocking类方法包含在过滤器之前,ruby-on-rails,rspec,mocha.js,Ruby On Rails,Rspec,Mocha.js,我正在尝试在从关注点调用\u filter之前测试。我的测试如下所示: class AuthorizableController < ApplicationController include Authorizable end describe Authorizable do let(:dummy) { AuthorizableController.new } it "adds a before filter to the class" do Authorizabl

我正在尝试在从关注点调用\u 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
…我得到了一个类似这样的错误(没有提到摩卡,而是MiniTest,当我使用RSpec时…):

故障:
1) Authorizable在类中添加before筛选器
失败/错误:AuthorizableController.expected(:在过滤器之前)。with(:authorize)
MiniTest::断言:
并非所有的期望都得到了满足
未满足的期望:
-预期仅一次,尚未调用:AuthorizableController.before\u筛选器(:authorize)
#./spec/controllers/concerns/authorizable_spec.rb:11:in'block(2级)in'

rails方法
class\u eval
对相关对象的单例类而不是具体类进行计算。关于什么是单例类,还有更多的解释。由于过滤器被添加到该单例类中,您对主类调用
authorize
的期望没有达到

您可以在singleton类上为
dummy
添加期望值:

dummy.singleton_class.expects(:before_filter).with(:authorize) 
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)>'
dummy.singleton_class.expects(:before_filter).with(:authorize)