Search code examples
ruby-on-railscucumbercapybara

Rails Cucumber Tests Getting Green on Red


I have a rather trivial set of Cucumber tests for a basic Rails application that will pass, despite the fact that I expect them to fail. The tests simply navigate to a static page and test for the presence of specific text on the page.

One of the Cucumber tests is as follows below:

Scenario: Visit the About screen
  Given I am on the About page
  Then I should see "Version"

The steps are defined as follows:

Given 'I am on the About page' do
  visit "/about"
end

Then 'I should see {string}' do |page_text|
  page.has_text?(page_text)
end

The page itself is defined in about.html.erb and has the following content

<h1>StaticPages#about</h1>
<p>Find me in app/views/static_pages/about.html.erb</p>

This test should clearly fail since it does not contain the text "Version", but it passes. How can I tell why the test is passing? What is the best approach for debugging these kinds of issues?


Solution

  • It's passing because you're not actually testing anything. page.has_text?(page_text) is just a predicate which returns true or false, instead you need to set an assertion which will raise an error in the case of failure. You don't indicate whether you are using RSpec or minitest, but something along the lines of

    expect(page).to have_text(page_text) # Rspec
    

    or

    assert_text(page_text) # minitest
    

    should be what you use