Forum: RSpec [RSpec] matcher for href

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
171ea139761951336b844e708d1547ab?d=identicon&s=25 James Byrne (byrnejb)
on 2009-01-19 20:11
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?

  <a href="/users/new">

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</a>\n\n</div>\n\r\n\r\n ...

So, I am nonplussed by the report that it did not find what was present.
171ea139761951336b844e708d1547ab?d=identicon&s=25 James Byrne (byrnejb)
on 2009-01-19 20:46
James Byrne 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]")
82476266af9d460415d8f1fc16bb54ed?d=identicon&s=25 Jarkko Laine (jarkko)
on 2009-01-19 21:20
(Received via mailing list)
On 19.1.2009, at 21.11, James Byrne 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?
>
>  <a href="/users/new">
>
> 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/clas...

//jarkko

--
Jarkko Laine
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
09477c358c5897d44121a248326e16d7?d=identicon&s=25 Jim Gay (Guest)
on 2009-01-19 21:36
(Received via mailing list)
On Jan 19, 2009, at 2:11 PM, James Byrne 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?
>
>  <a href="/users/new">

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/
This topic is locked and can not be replied to.