由于rubocop错误,我想避免在规格中使用allow_any_instance_of
。
我正在尝试更改此内容:
before do
allow_any_instance_of(ApplicationController).to receive(:maintenance_mode_active?).and_return(false)
end
it 'returns http success' do
expect(response).to have_http_status(:success)
end
根据rubocop-docs,它应该像这样:
let(:maintenance_mode?) { instance_double(ApplicationController) }
before do
allow(ApplicationController).to receive(:maintenance_mode_active?).and_return(false)
allow(maintenance_mode?).to receive(:maintenance_mode_active?)
end
it 'returns http success' do
expect(response).to have_http_status(:success)
end
我要测试的课程:
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
上面的代码有一个错误:
2.1) Failure/Error: allow(ApplicationController).to receive(maintenance_mode?).and_return(false)
#<InstanceDouble(ApplicationController) (anonymous)> received unexpected message :to_sym with (no args)
# ./spec/controllers/static_pages_controller_spec.rb:15:in `block (4 levels) in <top (required)>'
2.2) Failure/Error: allow(ApplicationController).to receive(maintenance_mode?).and_return(false)
TypeError:
[#<RSpec::Mocks::MockExpectationError: #<InstanceDouble(ApplicationController) (anonymous)> received unexpected message :to_sym with (no args)>] is not a symbol nor a string
# ./spec/controllers/static_pages_controller_spec.rb:15:in `block (4 levels) in <top (required)>'