1

I'm trying to get the syntax just right. I think I'm close but I'm trying to make a test to show that my admin can destroy a user. The error I get right now is :

Ambiguous match, found 2 elements matching link or button "delete

and I totally understand that. That means I get to the users index page and I see the delete button for a regular user and the admin. How do I exactly specify which user I want to click the delete button on so I can delete them in the test.

here is my users.yml file

user:
  name: user
  email: user@examle.com
  admin: false
  password_digest: <%= BCrypt::Password.create('password') %>

admin:
  name: admin
  email: admin@admin.com
  admin: true
  password_digest: <%= BCrypt::Password.create('password') %>

here is my admin_test.rb file

require "test_helper"

feature "as a blog owner I want a working user delete so I can remove trolls" do
  scenario 'admin can delete profile' do
    visit new_session_path
    fill_in 'Email', with: users(:admin).email
    fill_in 'password', with: 'password'
    click_on 'Log In'
    visit users_path(:admin)
    click_on 'delete'
    click_on 'OK'
    page.must_have_content "User deleted"
  end
end

I think the problem is caused on the line

click_on 'delete'

anyone know what the right syntax is or what I'm messing up?

  • 1
    Check this [SO Post](http://stackoverflow.com/questions/6733427/how-to-click-on-the-second-link-with-the-same-text-using-capybara-in-rails-3) – AbM Dec 20 '13 at 01:07

1 Answers1

0

First issue is that you probably don't want to be able to delete an admin. You also don't want to delete yourself. I would suggest adding an admin? boolean method to test for whether a user is an admin or not so that it doesn't show the delete option for admins. Something like this in your view would work then:

<% if current_user.admin? && !current_user?(user) %>
    | <%= link_to "delete", user, method: :delete,
                                  data: { confirm: "You sure?" } %>
<% end %>

Second is the issue when have two of the same element and get an ambiguous match. For testing a lot of times you only want to select one out of the many. You could just select the first one:

first('.item').click_link('Agree')

You can check out this answer for more info: How to click first link in list of items after upgrading to Capybara 2.0?

Community
  • 1
  • 1
ChrisBarthol
  • 4,871
  • 2
  • 21
  • 24