4
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【Rails tutorial】 リスト 9.25: [remember me] チェックボックスのテストでエラーになる。【9章】

Posted at

##エラー内容
Rails tutorial9章 リスト9.25を実施時に以下エラーとなりました。

 FAIL["test_login_without_remembering", UsersLoginTest, 4.873426502104849]
 test_login_without_remembering#UsersLoginTest (4.87s)
        Expected "I8qYEjEm7MeNLQbqt4YM3A" to be empty.
        test/integration/users_login_test.rb:52:in `block in <class:UsersLoginTest>'

チェックボックスのオンオフが効いてないようです。
セッションのコントローラーについて確認してみると・・・

app/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)
      remember(user)
      redirect_to user
    else
      flash.now[:danger] = 'Invalid email/password combination'
      render 'new'
    end
  end

remember(user)を消し忘れていたことが原因のようです。
これだとparams[:session][:remember_me] == '0'の場合でもremember(user)メソッドを実行しちゃいますね。

remember(user)を消してテストを実施。
今度はテスト成功しました!

4
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?