Rspec error: undefined local variable or method `cookies' for #<RSpec in the model for Sign_in method

前端 未结 2 452
执笔经年
执笔经年 2021-01-26 01:08

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[:r         


        
2条回答
  •  时光说笑
    2021-01-26 01:50

    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.

提交回复
热议问题