Maayan Naveh
Maayan Naveh

Reputation: 370

RSpec + Capybara scoping returns unexpected results

I have two tables: Suggestions and Keywords. I'm trying to test removal of items from Keywords (which puts it into Suggestions automatically).

Here's my test:

it "removes a chosen keyword" do
  page.first(:link, "Add").click
  within(:css, "#keywords") do
    find('li:nth-child(1)').click_link('X')
    expect(page).to have_no_content(item.search.term)
  end
end

It results in:

  1) New collection Suggestions removes a chosen keyword
     Failure/Error: expect(page).to have_no_content(item.search.term)
       expected not to find text "Gift for her" in "Keywords\nGift for her X"
     # ./spec/features/collections_spec.rb:60:in `block (4 levels) in <top (required)>'
     # ./spec/features/collections_spec.rb:53:in `block (3 levels) in <top (required)>'

Here's the print of the body element:

  <div class="col-lg-5 col-md-6 mb-4" id="suggestions">
    <div class="card h-100">
      <div class="card-body">
        <h4 class="card-title">Suggestions</h4>
        <p class="card-text"> 
          <ul class="list-group list-group-flush" id="suggestion-list">
                <li class="list-group-item">
                  Gift for her
                  <a class="btn btn-primary" rel="nofollow" data-method="post" href="/collections/1/item/choose.1">Add</a><a class="btn btn-primary" rel="nofollow" data-method="post" href="/collections/1/item/destroy.1">X</a><br>
                </li>
          </ul>
        </p>
      </div>
    </div>
  </div>

<div class="col-lg-5 col-md-6 mb-4" id="keywords">
    <div class="card h-100">
      <div class="card-body">
        <h4 class="card-title">Keywords</h4>
        <p class="card-text">                     
          <ul class="list-group list-group-flush" id="keyword-list">
          </ul>
        </p>
      </div>
    </div>
  </div>

And my show.html.erb file:

  <div class="col-lg-5 col-md-6 mb-4" id="suggestions">
    <div class="card h-100">
      <div class="card-body">
        <h4 class="card-title">Suggestions</h4>
        <p class="card-text"> 
          <ul class="list-group list-group-flush" id="suggestion-list">
            <% if @suggestions %>
                <% @suggestions.each do |s| %>
                <li class="list-group-item">
                  <%= Hpricot.uxs s.search.term %>
                  <%= link_to 'Add', collection_item_choose_path(@collection, s),{  method: :post, class: "btn btn-primary" } %><%= link_to 'X', collection_item_destroy_path(@collection, s),{  method: :post, class: "btn btn-primary" } %><br>
                </li>
              <% end %>
            <% end %>
          </ul>
        </p>
      </div>
    </div>
  </div>

<div class="col-lg-5 col-md-6 mb-4" id="keywords">
    <div class="card h-100">
      <div class="card-body">
        <h4 class="card-title">Keywords</h4>
        <p class="card-text">                     
          <ul class="list-group list-group-flush" id="keyword-list">
            <% if @keywords %>
              <% @keywords.each do |s| %>
                <li class="list-group-item">
                  <%= Hpricot.uxs s.search.term %>
                  <%= link_to 'X', collection_item_remove_path(@collection, s),{  method: :post, class: "btn btn-primary" } %><br>
                </li>
              <% end %>
            <% end %>
          </ul>
        </p>
      </div>
    </div>
  </div>

Seems to me like the scoping is off somehow, although I checked and the link does actually click (you can see it in the puts page.body). Any ideas? Thanks in advance!

Upvotes: 0

Views: 72

Answers (1)

Thomas Walpole
Thomas Walpole

Reputation: 49870

This is unlikely to be a scoping issue since the reported text is "Keywords\nGift for her X" - whereas if it were seeing the text in the "suggestions" section if would have "Add" text in there too.

More likely is the clicking on the 'X' just isn't getting processed by your app due to an error somewhere. Check your test.log to see exactly what methods are being called, and whether there are errors.

Additionally you don't mention what driver you're using with Capybara. If you're using a JS capable driver it's possible you don't have Capybara.default_max_wait_time set high enough for the hardware you're testing on and/or turbolinks (assuming you have that in your app) is getting involved. Again, looking at your test.log should give a better idea of what's actually happening. Another to try would be to use save_and_open_screenshot to get an image of what the actual page looks like (assuming you're using a driver that supports it)

If you're not using a JS capable driver (ie you're using rack_test) then it's possible you haven't required capybara/rails and the method of the links isn't being used. Again, checking test.log will show the method (get vs post) being used for each request.

Upvotes: 1

Related Questions