如何解决如何在rspec链接匹配器中检查`data-method`
我有一个RSpec测试,旨在确保网页上存在链接。
链接如下所示:
<a rel="nofollow" data-method="put" href="/change-user">Change user</a>
这是我的考试:
subject { page }
visit user_path(user)
it { should have_link('Change user',href: change_user_path(user)) }
我想在此测试中添加一个条件,以确保链接的值“ put”作为data-method
属性。
如何将这张支票添加到我的考试中?
解决方法
水豚提供different strategies来定位元素。
:element
selector允许您检查属性:
user_link = find_link('Change user',href: change_user_path(user))
expect(user_link).to match_selector(:element,'data-method' => 'put)
如果您打算经常这样做,则可以扩展:link
selector以支持:method
过滤器。
Capybara::Selector[:link].instance_eval do
expression_filter(:method) do |expression,value|
builder(expression).add_attribute_conditions('data-method': value)
end
describe_expression_filters do |**options|
if (method = options[:method])
" with method #{method.inspect}"
end
end
end
,然后将其用作:
should have_link('Change user',href: change_user_path(user),method: :put)
,
我发现可以将一个块传递给have_link
匹配器。将使用水豚发现的元素来调用此块。
在此块内,然后可以验证链接中的某些条件:
subject { page }
visit user_path(user)
it "shows the change user link" do
expect(subject).to have_link(
'Change user',href: change_user_path(user)
) { |link| link["data-method"] == "put" }
end
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。