Rspec错误:未定义的局部变量或方法`cookies'for#< RSpec在Sign_in方法的模型中 [英] Rspec error: undefined local variable or method `cookies' for #<RSpec in the model for Sign_in method
问题描述
我在我的应用程序中不使用任何gem进行身份验证...所以我的sign_in方法如下所示:
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 /支持/ helpers / session_module.rb
我需要在我的模型规范中调用sign_in(user)...我得到错误未定义的局部变量或方法`cookies'
推荐答案
cookies哈希只能在RSpec的控制器测试中使用。你可以告诉你的specs通过在你的上下文中添加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在Sign_in方法的模型中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!