It's easy to understand and convenient with capybara, but I'm addicted to fill_in, so I'll introduce one of the solutions.
An error occurs at fiii_in in expect.
I couldn't specify the id found in the verified and generated html, create a new id, or use find 。。
However, after several hours of fighting, the conclusion was reached.
Try to verify it as one of the fill_in error solutions!
In my case, I was getting an error because I couldn't log in successfully.
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
Here → 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)
I was clicking the link in the header instead of submit to specify the login with click_link. So I entered my email address and password on the login page and clicked on the login page again. .. What a barren thing. .. .. ..
So 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
Here → 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)
This passed properly.
I was only looking at fill_in because there was an error in fill_in, but I was able to reaffirm the importance of stopping and looking at the whole thing.
I hope it helps!