[RSpec] matcher for href


#1

Of the several ways available to test for this, what would be the
preferred way to see if the following is present in an html document
WITHOUT actually following the link?

I tried this:

response.body.should have_text(‘href="/users/new"’)

But this test fails, even though the rake features report is this:

expected “href=”/users/new"", got …

... </tr>\n\n</table>\n\n<br />\n\n<a href=\"/users/new\">New

user

\n\n\n\r\n\r\n …

So, I am nonplussed by the report that it did not find what was present.


#2

James B. wrote:

Of the several ways available to test for this, what would be the
preferred way to see if the following is present in an html document
WITHOUT actually following the link?

Apparently this is the correct form:

response.body.should have_tag(“a[href=/users/new]”)


#3

On 19.1.2009, at 21.11, James B. wrote:

Of the several ways available to test for this, what would be the
preferred way to see if the following is present in an html document
WITHOUT actually following the link?

I tried this:

response.body.should have_text(‘href="/users/new"’)

response.should have_tag(“a[href=/users/new]”)

http://rspec.rubyforge.org/rspec-rails/1.1.12/classes/Spec/Rails/Matchers.html#M000071

//jarkko


Jarkko L.
http://jlaine.net
http://dotherightthing.com
http://odesign.fi

Check out my latest book, Unobtrusive Prototype, fresh off the
Peepcode oven:
http://peepcode.com/products/unobtrusive-prototype-js


#4

On Jan 19, 2009, at 2:11 PM, James B. wrote:

Of the several ways available to test for this, what would be the
preferred way to see if the following is present in an html document
WITHOUT actually following the link?

response.should have_tag(‘a[href=?]’,’/users/new’)

I tried this:

response.body.should have_text(‘href="/users/new"’)

Or

response.should include_text(‘href="/users/new"’)

have_text should be used “when you want to match the whole string or
whole body”

include_text should be used “when you either don’t know or don’t care
where on the page this text appears.”

http://rspec.rubyforge.org/rspec-rails/1.1.12/