Ruby on rails 破裂模拟中的RSpec命名错误
在Ruby on rails 破裂模拟中的RSpec命名错误,ruby-on-rails,ruby,rspec,mocking,Ruby On Rails,Ruby,Rspec,Mocking,在ApplicationController中,我得到了一个方法,我想在maintenance\u mode\u controller\u specs中测试该方法的结果。如何创建mockmaintenance\u mode\u active方法,该方法将在规范中返回false ApplicationController.rb def maintenance_mode_active # ... feature_flag.active end 维护\u模式\u控制器\u规格 contex
ApplicationController
中,我得到了一个方法,我想在maintenance\u mode\u controller\u specs
中测试该方法的结果。如何创建mockmaintenance\u mode\u active
方法,该方法将在规范中返回false
ApplicationController.rb
def maintenance_mode_active
# ...
feature_flag.active
end
<代码>维护\u模式\u控制器\u规格
context 'when maintenance mode is inactive' do
let(:application_controller) { instance_double(ApplicationController) }
before do
allow(ApplicationController).to receive(:new).and_return(application_controller)
allow(application_controller).to receive(:maintenance_mode_active).and_return(false)
end
it 'redirect to root page' do
get :maintenance_mode
expect(response).to have_http_status(:redirect)
end
end
根据以上规格,我有一个错误:
故障/错误:功能\标志处于活动状态
命名错误:
nil:NilClass的未定义方法“active”
你是说?你表现得怎么样
这意味着mock不起作用。我会尝试使用:
我会尝试使用:
由于存根发生在维护模式控制器本身中,您应该尝试:
controller.stub(:maintenance_mode_active).and_return(false)
allow_any_instance_of
或expect_any_instance_of
被视为反模式,原因有很多。您可以阅读有关此的更多信息由于存根发生在维护模式控制器本身,您应该尝试:
controller.stub(:maintenance_mode_active).and_return(false)
allow_any_instance_of
或expect_any_instance_of
被视为反模式,原因有很多。你可以阅读更多关于这方面的内容