Rspec 错误:未定义局部变量或方法`cookies' for #<RSpec in the model for Sign_in 方法 [英] Rspec error: undefined local variable or method `cookies' for #<RSpec in the model for Sign_in method
问题描述
我没有在我的应用程序中使用任何 gem 进行身份验证...所以我的登录方法看起来像这样
I don't use any gem for authentication in my app...so my sign_in method looks like this
def sign_in(user)
remember_token = User.new_remember_token
cookies[:remember_token] = remember_token
user.update_attribute(:remember_token, User.encrypt(remember_token))
self.current_user = user
end
我在 app/helpers/session_helper.rb 和 spec/support/helpers/session_module.rb 下都有这个我需要在我的模型规范中调用 sign_in(user)...我收到错误 未定义的局部变量或方法`cookies' for
I have this under both app/helpers/session_helper.rb and spec/support/helpers/session_module.rb I need to call sign_in(user) in my model specs... I get the error undefined local variable or method `cookies' for
推荐答案
cookie 哈希仅在 RSpec 的控制器测试中可用.您可以通过在上下文中添加 type::request 来告诉您的规范假装它们是控制器规范,如下所示:
The cookies hash is only available in RSpec's controller tests. You can tell your specs to pretend that they're controller specs by adding type: :request to your context, like so:
describe 'something that requires cookies', type: :request do
it { expect(cookies[:remember_token]).to be_nil } // now the variable is defined as nil.
end
也就是说,需要您的模特了解登录信息让我感到惊讶.这种跨范围测试通常属于一个特性.
That said, needing your models to know about signing in surprises me. That kind of cross-scope testing usually belongs in a feature.
这篇关于Rspec 错误:未定义局部变量或方法`cookies' for #<RSpec in the model for Sign_in 方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!