Search code examples
ruby-on-railstestingcapybara-webkit

Rails Capybara Feature Tests with assert page.has_content? fails


I am trying to implement some feature test for my rails application using Capybara.

Now I am stucking.

I have an element in my html which looks like this:

<p class="no-stars">
  <strong class="">No stars:</strong>0
</p>

My test looks like this:

  test "should increase the number of stars for a user" do
    Capybara.current_driver = Capybara.javascript_driver
    visit users_path
    click_link('John F. Kennedy')
    assert page.has_content?('No stars: 0'), 'no matching content found!'
    page.find("input[value='+1']").click
    assert page.has_content?('No stars: 1'), 'no matching content found!'
  end

It seems to me that my test fails because it can't find the content 'No stars: 0'.

Does anyone have a solution for that problem?


Solution

  • In your html there is no space between the : and 0, but in your test there is a space which would cause a failure.

    As an aside, Capybara provides some assertions (assert_text, assert_selector, assert_no_text, ...) that can make things read a little better, and provide more detailed error messages. Rather than assert page.has_content... You can do

    page.assert_text('No stars:0')