gpt4 book ai didi

ruby-on-rails - 在 Rspec 的 API Controller 测试模块中出现委托(delegate)错误

转载 作者:太空宇宙 更新时间:2023-11-03 16:18:58 27 4
gpt4 key购买 nike

我正在编写一个 Controller 规范来验证这个私有(private)方法,我收到错误 Module::DelegationError: ActionController::RackDelegation 但我不知道如何解决这个问题。我找到的最好的例子是 http://owowthathurts.blogspot.com/2013/08/rspec-response-delegation-error-fix.html .

如何让未验证规范通过?我想确保返回 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。

最佳答案

如果有人正在处理编写 Controller 规范的类似问题,以下是我根据以下 2 个指南解决此问题的方法:http://codegur.com/22603728/test-user-authentication-with-rspechttps://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

关于ruby-on-rails - 在 Rspec 的 API Controller 测试模块中出现委托(delegate)错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38110920/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com