not_shitashi
not_shitashi

Reputation: 291

how to test html attributes with rspec?

the html I have is generated by ruby with:

<%= link_to "change", "http://gravatar.com/emails" %>

which results in:

<a href="http://gravatar.com/emails">change</a>

but I want to ensure that the link opens in a new tab with the

target="blank"

attribute

the rspec test looks like:

it { should have_link('change', href: 'http://gravatar.com/emails', target: '_blank') }

but the test still passes when I don't have the target attribute being generated.

Upvotes: 7

Views: 6708

Answers (2)

pward123
pward123

Reputation: 734

The following works for me with capybara 1.1.2:

it { should have_selector("a[href='http://gravatar.com/emails'][target='_blank']") }

Upvotes: 16

arieljuod
arieljuod

Reputation: 15838

I'm using the have_selector to handle every attribute:

should have_selector('a',
                     :href => 'http://gravatar.com/emails',
                     :target => '_blank',
                     :content => 'change'
                    )

I think you need capybara to have that matcher http://rubydoc.info/github/jnicklas/capybara/Capybara/RSpecMatchers/HaveSelector

Upvotes: 0

Related Questions