Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/52.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 RSpec如何在另一个方法中模拟方法_Ruby On Rails_Ruby_Rspec - Fatal编程技术网

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