2

My site had dozens of instances of "link_to". I decided to change most of them to "button_to" for better appearance. The lines of my test that used to check for the presence of those links are no good anymore.

assert_select "a", :href => user_path(@seminar.teacher), text: "Your Teacher Profile", count: 2

The test doesn't work correctly with

assert_select "button", etc...

Is there a way to adjust the test to find the button_to instances? From my initial searches, it seems like there isn't.

I'm also considering simply cutting those lines from the tests, since I've read in another article that simply testing for the presence of display items isn't a very useful way to use rails tests.

Thanks in advance for any insight!

Jeff Zivkovic
  • 547
  • 1
  • 4
  • 20
  • You can also add little style to link_to to look like button. (But if you add button_to it will make post request by default where as link_to does get request by default. ) – pk-n Aug 27 '16 at 15:54
  • I tried the styling at first. But I was struggling with the appearance of the link after it had been visited. Do you suggest going back to "link_to" if I can get the visited styling to work correctly? – Jeff Zivkovic Aug 28 '16 at 18:45

3 Answers3

2

Here is one way to test for the presence of button_to using Rails' default testing framework MiniTest. In this example there is a button on a member's show page to change their membership status to inactive:

<%= button_to 'Change status to Inactive', membership_path(@membership), method: :patch, 
    params: { 'membership[status]' => 'Inactive'} %>

If you inspect your view in a web browser to see the HTML (in Chrome, right click the button element then select Inspect) you'll see that button_to is actually a form. So you can test for the existence of a form element with an action property equal to the specific membership_path.

membership = Membership.first
assert_select 'form[action=?]', membership_path(membership.id)

If you want to also test that the button's text is present then do a block

membership = Membership.first
assert_select 'form[action=?]', membership_path(membership.id) do
  assert_select 'input[value=?]', 'Change status to Inactive'
end
Steve Carey
  • 2,876
  • 23
  • 34
1

Updated for Rails 7, and likely 6, system tests.

assert_button "Click me"
Carson Cole
  • 4,183
  • 6
  • 25
  • 35
-1

I would remove those tests that just test for the presence of a link. It would be better to write tests that click those buttons and test what happens after the button is clicked. Users care more about what happens when a button is clicked than the presence of a button on a page.

kcdragon
  • 1,724
  • 1
  • 14
  • 23
  • I've been looking for ways to get my tests to simulate clicking the buttons on the pages. Do you happen to know of an article or tutorial that addresses this type of testing? Or do you know the syntax for such a test so that I can search for articles? – Jeff Zivkovic Aug 28 '16 at 18:44
  • 1
    Capybara is a popular tool to test this – kcdragon Aug 28 '16 at 19:04
  • Yes. Capybara looks like exactly what I'm looking for. Thank you! – Jeff Zivkovic Aug 31 '16 at 01:06