Ad

RSpec/Capybara "expect(page).to Have_content" Test Fails While Content Is In Page.html

- 1 answer

I am writing a Rails app applying BDD using RSpec & Capybara. One of my tests continues to fail. The goal of the test is to check whether for each Machine record displayed on the index page, clicking on the edit link, results in visualising the details edit page. When I run my application, this functionality works. So I guess, there's something wrong with my RSpec scenario.

Here's the failing test:

Failures:

  1) existing machines have a link to an edit form
     Failure/Error: expect(page).to have_content(@existing_machine.model)
       expected to find text "RX22" in "Toggle navigation uXbridge Catalogue Settings Brands Machine Types Machine Groups Repair States Titles User Signed in as [email protected] Sign out Machine details Brand TORO Model Machine type ZITMAAIER Description Engine Purchase Price Unit Price VAT Minimal Stock Current Stock Warehouse Location"
 # ./spec/features/machine_spec.rb:50:in `block (2 levels) in <top (required)>'

Here's the code of the test:

RSpec.feature 'existing machines' do
  before do
    @john = User.create!(email: '[email protected]', password: 'password')

    login_as @john
    brand = Brand.create!(name: 'TORO')
    machinegroup = Machinegroup.create!(name: 'GAZON')
    machinetype = Machinetype.create!(name: 'ZITMAAIER', machinegroup_id: machinegroup.id)
    @existing_machine = Machine.create!(brand_id: brand.id, model: 'RX22', machinetype_id: machinetype.id, description: 'fantastic machine', engine: '100PK' )
  end

  scenario 'have a link to an edit form'  do
    visit '/machines'
    find("a[href='/machines/#{@existing_machine.id}/edit']").click
    expect(page).to have_content('Machine details')
    expect(page).to have_content(@existing_machine.model)
  end
end

When debugging the scenario, the @existing_machine object seems correctly populated through the .create!() method in the before do block.

screenshot of debug window in IDE

When inspecting the page.html in the debugger, I do see the "RX22" string appearing.

screenshot of page.html inspection

So why does RSpec/Capybara not see the same content when executing the expect(page).to have_content(@existing_machine.model)?

Ad

Answer

RX22 is the value of an input element not text content so you need to check for it differently. Something like

expect(page).to have_field('Model', with: 'RX22')

should work

Ad
source: stackoverflow.com
Ad