Reputation: 366
I'm using RSpec (v. 3.1.0) and Capybara (v. 2.4.3) to test my Rails 4 application. My user delete spec.rb contains the following code:
it "is successful when clicking the destroy link" do
visit "/users"
save_and_open_page
within "#user_#{user.id}" do
click_link "Destroy"
end
expect(page).to have_content("User was successfully destroyed.")
Capybara throws the following error:
1) Deleting User is successful when clicking the destroy link
Failure/Error: within "#user_#{user.id}" do
Capybara::ElementNotFound:
Unable to find css "#user_509"
Save_and_open_page shows FactoryGirl created a user for the test. The error message indicates the user's id is 509. My program's code (delete method) works as expected, so the problem appears to be my test code. What can I do to help Capybara find the user? Thanks for any help!
UPDATE: Here's the HTML source:
<!DOCTYPE html>
<html>
<head>
<title>Users | App </title>
<link data-turbolinks-track="true" href="/assets/appointments.css?body=1" media="all" rel="stylesheet" />
<link data-turbolinks-track="true" href="/assets/cardio_exercises.css?body=1" media="all" rel="stylesheet" />
<link data-turbolinks-track="true" href="/assets/custom.css?body=1" media="all" rel="stylesheet" />
<link data-turbolinks-track="true" href="/assets/members.css?body=1" media="all" rel="stylesheet" />
<link data-turbolinks-track="true" href="/assets/sessions.css?body=1" media="all" rel="stylesheet" />
<link data-turbolinks-track="true" href="/assets/static_pages.css?body=1" media="all" rel="stylesheet" />
<link data-turbolinks-track="true" href="/assets/strength_exercises.css?body=1" media="all" rel="stylesheet" />
<link data-turbolinks-track="true" href="/assets/trainers.css?body=1" media="all" rel="stylesheet" />
<link data-turbolinks-track="true" href="/assets/users.css?body=1" media="all" rel="stylesheet" />
<link data-turbolinks-track="true" href="/assets/application.css?body=1" media="all" rel="stylesheet" />
<script data-turbolinks-track="true" src="/assets/jquery.js?body=1"></script>
<script data-turbolinks-track="true" src="/assets/jquery_ujs.js?body=1"></script>
<script data-turbolinks-track="true" src="/assets/bootstrap.js?body=1"></script>
<script data-turbolinks-track="true" src="/assets/turbolinks.js?body=1"></script>
<script data-turbolinks-track="true" src="/assets/appointments.js?body=1"></script>
<script data-turbolinks-track="true" src="/assets/cardio_exercises.js?body=1"></script>
<script data-turbolinks-track="true" src="/assets/members.js?body=1"></script>
<script data-turbolinks-track="true" src="/assets/sessions.js?body=1"></script>
<script data-turbolinks-track="true" src="/assets/static_pages.js?body=1"></script>
<script data-turbolinks-track="true" src="/assets/strength_exercises.js?body=1"></script>
</script>
<div class="container">
<h1>Listing Users</h1>
<table>
<thead>
<tr>
<th>User Name</th>
<th>Email</th>
<th>Member</th>
<th>Trainer</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<tr>
<td>ColeCole</td>
<td>[email protected]</td>
<td>true</td>
<td>false</td>
<td><a href="/users/14">Show</a></td>
<td><a href="/users/14/edit">Edit</a></td>
<td><a data-confirm="Are you sure?" data-method="delete" href="/users/14" rel="nofollow">Destroy</a></td>
</tr>
</tbody>
</table>
<br>
<a href="/users/new">New User</a>
<footer class="footer">
<small>
The <a href="https://xxxx.herokuapp.com/">xxxx</a>
by <a href="https://xxxx.herokuapp.com/">xxxx</a>
</small>
<nav>
<ul>
<li><a href="/contact">Contact</a></li>
<li><a href="/about">About</a></li>
<li><a href="https://xxxx.herokuapp.com/#">xxxx</a></li>
</ul>
</nav>
</footer>
<pre class="debug_dump">--- !ruby/hash:ActionController::Parameters
action: index
controller: users
</pre>
</div>
</body>
</html>
Upvotes: 0
Views: 1031
Reputation: 1668
This should work if you only have one delete button on the page:
it "is successful when clicking the destroy link" do
visit "/users"
click_link "Destroy"
expect(page).to have_content("User was successfully destroyed.")
Edit: I'm going to assume you are not very familiar with CSS selectors. With Capybara, when you say:
#user_#{user.id}
You are not grabbing text such as "user_509" but rather you are grabbing a DOM element with the identity of "user_509." Thus to have Capybara find that element in your code, you would have to have something like this:
<a id="user_509" data-confirm="Are you sure?" data-method="delete" href="/users/14" rel="nofollow">Destroy</a>
Merely having a text field would not suffice as text is not part of the DOM.
Upvotes: 1