LoginSignup
0
1

More than 3 years have passed since last update.

【rspec】fill_inがあっているのにfetureテストが失敗する原因 ログインで失敗してない?

Last updated at Posted at 2020-07-14

rspecのfeatureテストが通らない

capybaraでわかりやすくて便利なのですが、fill_inハマったので解決策の1つを紹介します。

expect内のfiii_inのところでどうしてもエラーが起きてしまう。

検証して生成されたhtmlから見つけたidを指定しても、新たにidを作っても、findを使ってもできませんでした
。。

しかし数時間の格闘の末結論が出ました。

この記事のゴール

fill_inのエラー解決策の1つとして検証してみてください!

結論 ログインに失敗している

自分の場合は結論ログインがうまくできていないためエラーが起きていました。

失敗したコード

topics_spec.rb
scenario "user creates a new topic" do
    user = FactoryBot.create(:user)
    visit root_path
    click_link "ログイン"
    fill_in "Email", with: user.email
    fill_in "Password", with: user.password
ココ→ click_link "ログイン"

    expect {
      visit new_topic_path
      fill_in "topic_description", with: "test"
      click_button "投稿"

      expect(page).to have_content "投稿しました"
      expect(page).to have_content "test"
      expect(page).to have_content "#{user.name}"
    }.to change(user.topics, :count).by(1)

スクリーンショット 2020-07-14 9.39.41.png

click_linkでログインを指定ためsubmitではなくヘッダーのリンクをクリックしていました。
そのためログインページでメールアドレスとパスワードを入力して、もう一度ログインページをクリックしていました。。
なんと不毛なことを。。。。

ということでclick_linkではなく、click_buttonを指定。

topics_spec.rb
scenario "user creates a new topic" do
    user = FactoryBot.create(:user)
    visit root_path
    click_link "ログイン"
    fill_in "Email", with: user.email
    fill_in "Password", with: user.password
ココ→ click_button "ログイン"

    expect {
      visit new_topic_path
      fill_in "topic_description", with: "test"
      click_button "投稿"

      expect(page).to have_content "投稿しました"
      expect(page).to have_content "test"
      expect(page).to have_content "#{user.name}"
    }.to change(user.topics, :count).by(1)

これでちゃんと通りました。

まとめ

fill_inでエラーが起きているのでfill_inばかり見ていましたが、一度立ち止まって全体を見ることの重要性を再認識できました。

参考になれば嬉しいです!

0
1
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
0
1