我正在编写一个控制器规范来验证这个私有方法,但我得到了错误Module::DelegationError: ActionController::RackDelegation
,但我很失落,因为如何解决这个问题。我找到的最好的例子是http://owowthathurts.blogspot.com/2013/08/rspec-response-delegation-error-fix.html。
如何通过unverified
规范?我想确保401被退回。
方式
def validate_api_request
return four_oh_one unless api_request_verified?(request)
end
当前规范
describe Api::ApiController, type: :controller do
describe '#validate_api_request' do
it 'verified' do
allow_any_instance_of(described_class).to receive(:api_request_verified?).and_return(true)
expect(subject.send(:validate_api_request)).to be_nil
end
it 'unverified' do
allow_any_instance_of(described_class).to receive(:api_request_verified?).and_return(false)
allow(controller).to receive(:redirect_to)
binding.pry
end
end
end
我正在使用Rails 4。
答案 0 :(得分:1)
如果有人正在处理编写控制器规范的类似问题,请按以下两个指南解决此问题:http://codegur.com/22603728/test-user-authentication-with-rspec和https://gayleforce.wordpress.com/2012/12/01/testing-rails-before_filter-method/。
describe Api::ApiController, type: :controller do
describe '#validate_api_request' do
controller(Api::ApiController) do
before_filter :validate_api_request
def fake
render text: 'TESTME'
end
end
before do
routes.draw { get 'fake', to: 'api/api#fake' }
end
it 'verified' do
allow_any_instance_of(described_class).to receive(:api_request_verified?).and_return(true)
expect(subject.send(:validate_api_request)).to be_nil
end
it 'unverified' do
allow_any_instance_of(described_class).to receive(:api_request_verified?).and_return(false)
get 'fake'
expect(response.status).to be(401)
end
end
end