Ruby on rails RSpec如何在另一个方法中模拟方法
在Ruby on rails RSpec如何在另一个方法中模拟方法,ruby-on-rails,ruby,rspec,Ruby On Rails,Ruby,Rspec,在application\u controller中,我有两种方法,我想在maintenance\u mode\u controller\u specs中测试它们的结果。如何创建模拟维护\u模式\u活动?该模式将返回false,以便在检查维护?中使用它 application\u controller.rb 行动前:检查维护 private def check_maintenance? if maintenance_mode_active? == true redirect_to
application\u controller
中,我有两种方法,我想在maintenance\u mode\u controller\u specs
中测试它们的结果。如何创建模拟维护\u模式\u活动?
该模式将返回false
,以便在检查维护?
中使用它
application\u controller.rb
行动前:检查维护
private
def check_maintenance?
if maintenance_mode_active? == true
redirect_to maintenance_mode
elsif request.fullpath.include?(maintenance_mode_path)
redirect_to :root
end
end
def maintenance_mode_active?
# do sth ...
mode.active?
end
maintenance\u mode\u controller\u spec.rb
context 'when maintenance mode is active' do
let(:maintenance_mode?) { instance_double(ApplicationController) }
before do
allow(ApplicationController).to receive(:maintenance_mode_active?).and_return(false)
end
it 'redirect to root path' do
expect(described_class).should redirect_to(maintenance_mode_path)
end
end
maintenance\u mode\u active
是一种实例方法,您可以在类级别上存根它。你需要使用
由于rubocop的原因,我无法使用
允许任何的实例,有其他选择吗?
before do
allow_any_instance_of(ApplicationController).to receive(:maintenance_mode_active?).and_return(false)
end