how to test html attributes with rspec?

十年热恋 提交于 2019-12-30 17:57:48

问题


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.


回答1:


The following works for me with capybara 1.1.2:

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



回答2:


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



来源:https://stackoverflow.com/questions/11313725/how-to-test-html-attributes-with-rspec

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!