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

B Seven picture B Seven · Aug 17, 2011 · Viewed 24.3k times · Source

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.

Answer

Chris Houhoulis picture Chris Houhoulis · Jun 28, 2012

In assert_select, you can also use a question mark for the attribute value, and then follow with a string to match.

assert_select "a[href=?]", "/desired_path/1"

There's another way to use assert_select that is more friendly, especially if you want to match a partial string or regexp pattern:

assert_select "a", :href => /acebook\.com\/share/