Question

I have a link as follows that I'm trying to test (please ignore the square brackets):

[%= link_to "Delete User", destroy_user_account_path(@profile.user), :class => "delete", :confirm => "a", :title => "Delete #{@profile.user.name}", :method => :delete %]

The test below fails, though if I comment out the :confirm => "a" line, it passes:

  it "should have a link to delete the user's account (using the destroy_user_account action in the registrations controller)" do
    get :show, :id => @profile
    response.should have_selector("a",
                                  :href => destroy_user_account_path(@profile.user),
                                  :confirm => "a",
                                  :title => "Delete #{@profile.user.name}",
                                  :class => "delete", 
                                  :content => "Delete User")
  end

Behold my failure :(

 Failure/Error: response.should have_selector("a",
   expected following output to contain a <a title='Delete Michael Hartl' class='delete' href='/destroy-user-account/159' confirm='a'>Delete User</a> tag:

The actual html output for this line is as follows (again, the square brackets are mine). I note it's output "data-confirm" as an attribute in here rather than the "confirm" one that the test is expecting.

[a href="/destroy-user-account/159" class="delete" data-confirm="a" data-method="delete" rel="nofollow" title="Delete Michael Hartl"]Delete User[/a]

Can anyone explain what the difference is between confirm and data-confirm in this context, and help me figure out why I'm getting this error/how to fix it?

Thanks!

Was it helpful?

Solution

"Confirm" is not an HTML attribute. data-whatever tags are an HTML5 feature that allows you to put whatever custom attributes you want on an element, mainly to pass information to and from Javascript on the client side.

So: <a confirm="foo"></a> is not valid HTML, but <a data-confirm="foo"></a> is.

The Rails UJS looks for data-confirm tags and knows to prompt you with a confirmation message if you click them. It takes the confirmation message from the data-confirm value.

So, in this case your code should read:

response.should have_selector("a",
                              :href => destroy_user_account_path(@profile.user),
                              'data-confirm' => "a",
                              :title => "Delete #{@profile.user.name}",
                              :class => "delete", 
                              :content => "Delete User")

That should take care of your issue, let me know if it doesn't.

OTHER TIPS

"Confirm" option is just an alias for "data-confirm" that link_to provides.

link_to anything, :confirm => "Message" # is equivalent to
link_to anything, 'data-confirm' => "Message"

But the matcher you use doesn't know the alias so you need to use "data-confirm" there:

response.should have_selector("a",
                              :href => destroy_user_account_path(@profile.user),
                              'data-confirm' => "a",
                              :title => "Delete #{@profile.user.name}",
                              :class => "delete", 
                              :content => "Delete User")
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top