[英]Stub global cookies variable in Rspec Rails test
The Rspec test looks like this: Rspec测试如下所示:
RSpec.describe Api::UsersController, type: :controller do
describe 'POST #create' do
subject { post :create, params: create_params }
context '...' do
In the controller, I use: 在控制器中,我使用:
cookies.permanent[:foo] == 'bar'
However, I'm getting: 但是,我得到:
NameError:
undefined local variable or method `cookies' for #<Api::UsersController:0x000000137702f0>
Another SO question has an answer that says to add type: :request
to describe
, but this causes other errors. 另一个SO问题的回答是添加
type: :request
to describe
,但这会导致其他错误。 I'd rather just completely stub out cookies
so cookies.permanent[:foo] == 'bar'
is always false. 我宁愿完全
cookies.permanent[:foo] == 'bar'
掉cookies
所以cookies.permanent[:foo] == 'bar'
总是错误的。 How do I do that? 我怎么做?
By default controllers in rails-api don't include the middleware that handles cookies. 默认情况下,rails-api中的控制器不包括处理cookie的中间件。 If you need cookies then you need to add that middleware:
如果您需要Cookie,则需要添加该中间件:
config.middleware.use ActionDispatch::Cookies
For the above problem, you need to include 对于上述问题,您需要包括
ActionController::Cookies
in your spec files 在您的规格文件中
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.