user1874182 user1874182 - 1 month ago 9
Ruby Question

Ruby code in my html.erb file not executed during Cucumber test

I'm creating a Ruby on Rails web app and have an index.html.erb file for one of my views that contains the following code:

<div class="container" id="quickTickets">
<% Game.all.each do |game| %>
<h2>
<%= link_to game.name, game_tickets_path(game.id), :id => "gameid_"+game.id.to_s %>
</h2>
<%end%>
</div>


The code works correctly when I view it in a browser and displays a header for each game that links to a separate page. However, when I run a Cucumber/Capybara test to simulate clicking on one of the headers, it fails.

The test fails because the block of Ruby code above is not executed when the Cucumber test is run, so the page ends up containing an empty
<div>
.

For example, when I make a call to
print page.html
to examine the HTML my Cucumber test is operating on, it displays the following:

<div class="container" id="quickTickets">
</div>


I've spent hours trying to find any documentation or previous bugs like this but haven't found a solution. Any ideas?

Answer

Since the container div is in the output the code is being evaluated. Whats likely happening is that you actually don't have Game objects to show. This is either because you haven't created any Game objects in the test, or because you are using a JS capable driver and haven't configured your tests to not use transactions (https://github.com/jnicklas/capybara#transactions-and-database-setup)