Dave Olson
Dave Olson

Reputation: 252

Why won't this rspec test pass?

I've spent the last hour trying to figure out why this test won't pass.

I'm working in Rails 4.0 with Capybara 2.1.0 and rspec 2.14.1

Here's my test:

require 'spec_helper'

describe "Stats" do
subject { page }
describe "Index Page" do
    before { visit stats_path }
    it { should have_selector 'h1', text: 'Stats' }
end
end

And my index.html.erb file:

<h1>Stats</h1>
<table>
  <thead>
<tr>
  <th>Attendance</th>
  <th>Salvations</th>
  <th>Visitors</th>
  <th>Offering</th>
  <th>Service Date</th>
  <th>Time</th>
  <th>Campus</th>
  <th></th>
  <th></th>
  <th></th>
</tr>
  </thead>

  <tbody>
    <% @stats.each do |stat| %>
      <tr>
     <td><%= stat.attendance %></td>
    <td><%= stat.salvations %></td>
    <td><%= stat.visitors %></td>
    <td><%= stat.offering %></td>
    <td><%= stat.date %></td>
    <td><%= stat.time %></td>
    <td><%= stat.campus.name %></td>
    <td><%= link_to 'Show', stat %></td>
    <td><%= link_to 'Edit', edit_stat_path(stat) %></td>
    <td><%= link_to 'Destroy', stat, method: :delete, data: { confirm: 'Are you sure?' } %></td>
  </tr>
<% end %>
 </tbody>
</table>

<br>

<%= link_to 'New Stat', new_stat_path %>

And the error I'm getting is:

1) Stats Index Page should have css "h1" with text "Stats" Failure/Error: it { should have_selector 'h1', text: 'Stats' } Capybara::ExpectationNotMet: expected to find css "h1" with text "Stats" but there were no matches # ./spec/features/stats_spec.rb:7:in `block (3 levels) in '

I'm stumped. Any help would be appreciated.

Incidentally, my index page renders correctly without any errors.

Upvotes: 0

Views: 168

Answers (1)

Benjamin Bouchet
Benjamin Bouchet

Reputation: 13181

If it works when you visit then page but not when you run your test script then there is certainly a session or authentication matter. Is your index page protected by any authentication or authorization logic?

I cannot give you a solution, but I can try to give you a guide line, if you haven't do it yet, use pp to output to the console the rendering of the page.

For example, if you are testing in controllers directory, use pp response.body. Or if you are testing in views directory, use render followed by pp rendered.

Then from the output in your console you will certainly get a clue according to your specific logic on what's append.

Upvotes: 0

Related Questions