Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading