[Ruby] [Rspec] Cause of failing test even though fill_in is correct Cause of login failure?

1 minute read

rspec feature test fails

capybara is easy to understand and convenient, but since I was addicted to fill_in, I will introduce one of the solutions.

An error always occurs at fiii_in in expect.

I was not able to specify the id found from the html generated by verification, create a new id, or use find .. ..

But after a few hours of struggle, the conclusion was reached.

Goal of this article

Please verify as one of the fill_in error solutions!

Conclusion Login failed

In my case, the conclusion was that the login was not successful and the error occurred.

Failed code

topics_spec.rb


scenario "user creates a new topic" do
    user = FactoryBot.create(:user)
    visit root_path
    click_link "Login"
    fill_in "Email", with: user.email
    fill_in "Password", with: user.password
Coco → click_link "Login"

    expect {
      visit new_topic_path
      fill_in "topic_description", with: "test"
      click_button "Post"
      
      expect(page).to have_content "Posted"
      expect(page).to have_content "test"
      expect(page).to have_content "#{user.name}"
    }.to change(user.topics, :count).by(1)

Screenshot 2020-07-14 9.39.41.png

I was clicking the link in the header instead of submit because I specified the login with click_link. So I used to enter my email address and password on the login page and click the login page again. .. What a barren thing. .. .. ..

Therefore, specify click_button instead of click_link.

topics_spec.rb


scenario "user creates a new topic" do
    user = FactoryBot.create(:user)
    visit root_path
    click_link "Login"
    fill_in "Email", with: user.email
    fill_in "Password", with: user.password
Coco → click_button "Login"

    expect {
      visit new_topic_path
      fill_in "topic_description", with: "test"
      click_button "Post"
      
      expect(page).to have_content "Posted"
      expect(page).to have_content "test"
      expect(page).to have_content "#{user.name}"
    }.to change(user.topics, :count).by(1)

I passed this properly.

Summary

I was just looking at fill_in because there was an error in fill_in, but I was able to realize the importance of stopping once and seeing the whole.

I would be happy if it could be helpful!