How to access remember_token virtual attribute in integration test?

I am working on michal hart Ruby in a rail guide in chapter 8. Click here for details , I'm stuck in exercise 8.6. The author presented a way to access the remember_token virtual attribute in the integration test, and he left some work for us,

assert_equal assigns(:user).FILL_IN, FILL_IN 

I have to replace the correct code with the FILL_IN placeholder. I just couldn't think about the right one. I tried using

 assert_equal assigns(:user).cookies, remember_token 

application / controllers / sessions_controller.rb

 def create @user = User.find_by(email: params[:session][:email].downcase) if @user && @user.authenticate(params[:session][:password]) log_in @user params[:session][:remember_me] == '1' ? remember(@user) : forget(@user) redirect_to @user 

But that did not work, Erros:

  "test_login_with_remembering", UsersLoginTest, 0.590876] test_login_with_remembering#UsersLoginTest (0.59s) NoMethodError: NoMethodError: undefined method `cookies' for #<User:0x007f964f1a91d0> 

I know that this is very simple, I really did some research, it seems no one asked the same question, because the textbook is completely new. Please understand that I'm just a beginner, I would really appreciate it if you could help me solve this problem.

+6
source share
2 answers

I have an answer by buying Michael's book. And the answer is included in the decision guide. I think it's worth sharing since I asked this question

 assert_equal assigns(:user).remember_token, cookies['remember_token'] 

It doesn’t cost anything that reading about the ruby ​​language will be very useful for understanding this code. My problem is that I didn’t read much about Ruby before diving into rail research, but that’s not a big problem, I just switched to Ruby to get a deeper understanding of the Ruby language! Good luck.

+12
source

I got an error when I tried as @Snailwalker

assert_equal assigns(:user).remember_token, cookies['remember_token']

But it works as follows

assert_equal assigns[:user].remember_token, cookies['remember_token']

0
source

Source: https://habr.com/ru/post/979379/


All Articles