简体   繁体   English

如何使用 rspec 测试 html 属性?

[英]how to test html attributes with rspec?

the html I have is generated by ruby with:我拥有的 html 是由 ruby​​ 生成的:

<%= 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: rspec 测试如下所示:

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.1.2:

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

I'm using the have_selector to handle every attribute:我正在使用 have_selector 来处理每个属性:

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我认为你需要水豚才能拥有那个匹配器http://rubydoc.info/github/jnicklas/capybara/Capybara/RSpecMatchers/HaveSelector

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM