adam.szczombrowski
adam.szczombrowski

Reputation: 23

Rails. Capybara fill_in finds text inputs but doesn't fill_in

So I have this feature spec in my Rails app

require 'rails_helper'

feature 'As a signed in user' do
  let(:user) {create(:user)}
  let(:article) { create(:article)}
  before {login_as(user, :scope => :user )}

  scenario 'I can edit article with valid attributes' do
    visit edit_article_path(article)
    puts current_path
    fill_in 'article_name', with: 'Valid name'
    fill_in 'article_content', with: 'Valid content'
    # save_and_open_page
    click_button 'Update Article'
    expect(article.name).to eq 'Valid name'
  end
end

and fill_in doesn't actually fill the input fields with Valid name and Valid content. I debug it by saving and opening page and the values remain the same so I guess it's not problem with my Rails app but rather something with Capybara. In other future spec:

require 'rails_helper'

feature 'As a signed in user' do
  let(:user) {create(:user)}
  let(:article) { build(:article)}
  before {login_as(user, :scope => :user )}

  scenario 'I can create article with valid attributes' do
    visit '/articles/new'
    fill_in 'Name', with: article.name
    fill_in 'article_content', with: article.content
    expect {click_button 'Create Article'}.to change {Article.count}.by(1)
  end
end

everything works as expected. The error I am getting is:

Failures:

  1) As a signed in user I can edit article with valid attributes
     Failure/Error: expect(article.name).to eq 'Valid name'

       expected: "Valid name"
            got: "Name1"

       (compared using ==)
     # ./spec/features/articles/article_update_spec.rb:15:in `block (2 levels) in <top (required)>'

What can be the reason for it and how to make a spec pass?

Upvotes: 2

Views: 308

Answers (1)

Thomas Walpole
Thomas Walpole

Reputation: 49890

It appears you're using the rack-test driver since your test isn't tagged as js: true. Assuming that is true, your issue is that article is already in memory, and is not reloaded from the DB before checking for the name change. Updating your test to the following will force a reload and then your test should pass

expect(article.reload.name).to eq 'Valid name'

If you're not using the rack-test driver then click_button will be asynchronous and you'll have other issues since you're not checking for changes in the browser before checking for database object changes.

Upvotes: 1

Related Questions