Krawalla
Krawalla

Reputation: 198

Rspec testing, Capybara unable to find link or button

This the part of my requests test that fails:

scenario 'Admin destroys a job posting + gets notified' do
  parent = create(:parent)  
  create(:assignment, user_id: @user.id, role_id: 1)
  demand = create(:demand, shift_id: 4)
  sign_in(@user)
  visit demands_path

  click_on 'Destroy'
  expect(page).to have_content('successfully')
end

This is the error:

 Failure/Error: click_on 'Destroy'

 Capybara::ElementNotFound:
   Unable to find link or button "Destroy"

And here is the corresponding index view, including a "Destroy" link in the app:

enter image description here

Any idea why this test fails??

Upvotes: 3

Views: 5198

Answers (1)

Thomas Walpole
Thomas Walpole

Reputation: 49870

Odds are the data you assume is on the page actually isn't. This could be for a number of reasons.

  1. Your page requires JS and you're not using a JS capable driver - see https://github.com/teamcapybara/capybara#drivers

  2. Your sign_in method is defined to fill in user/pass and then click a button, but doesn't have an expectation for content that confirms the user has completed login at the end. This can lead to the following visit occurring before login has completed and therefore not actually logging in. Verify that by inspecting the result of page.html or calling page.save_and_open_screenshot before the click.

  3. Your 'Destroy' "button" is neither an actual <a> element or <button> element. Fix that by either using semantic markup or swapping to find(...).click

  4. You are using a JS capable driver but your records aren't actually visible to the app - this would affect all your tests though so I assume it's probably not this. If this was the case the login would fail and you'd probably need to install database_cleaner and configure for use with RSpec & Capybara - https://github.com/DatabaseCleaner/database_cleaner#rspec-with-capybara-example

Upvotes: 4

Related Questions