ruby-on-railsruby-on-rails-3testingminitesttestunit

How to use assert_select to test for presence of a given link?


My page should contain a link that looks like <a href="/desired_path/1">Click to go</a>.

How would you test for that using assert_select? I want to check for the presence of an a tag with href="/desired_path/1". How do you test the attributes of a tag?

Are there any resources to explain how to use assert_select? I read the Guides and API documentation, but didn't figure it out. Are there any recommended better ways of doing this?

I am working in Rails 3 and using the built-in test framework.

Thanks.


Solution

  • You can pass any CSS selector to assert_select. So to test the attribute of a tag, you use [attrname=attrvalue]:

    assert_select("a[href=/desired_path/1]") do |elements|
       # Here you can test that elements.count == 1 for instance, or anything else
    end