我目前正在测试带有 Rspec 请求的嵌套路由的发布请求。
设置如下:
describe 'POST /url_contents' do
let!(:role) { create(:role, name: "admin") }
let(:valid_attributes) {
{
jwt: "SAMPLE",
role: role
}
}
context 'when the url is valid' do
before { post "/#{role.name}/1", params: valid_attributes }
it 'returns a status code of 201' do
expect(response).to have_http_status(201)
end
end
end
我正在点击控制器,但是当我检查参数时,我得到了一个嵌套参数:
:params => { :params => { :jwt => "SAMPLE", :role => #<Role:0x0055959d32a888>" }, "controller"=> "auth"... }
如何让参数指向我的控制器的
:jwt
?
我找到了解决方案,但很高兴听到其他想法。
context 'when the jwt is valid' do
before { post "/#{role.name}/1", {
jwt: "SAMPLE",
role: role
}
}
现在 params 不再嵌套。
确保您调用正确的方法(
post
)。
我曾多次看到一个项目中有多个 post
方法。
有一次我遇到了一个使用 sinatra gem 中的方法的案例。
我还看到有人复制到 rspec test 中得到了 include Rack::Test::Methods 行。
我使用 pry 进行此类测试。在撬开时,您可以使用show-method get
。在 rails
的情况下,它应该返回类似 Rails::Controller::Testing::Integration
的内容