如何断言(删除)路径 - 图像组合不存在

时间:2015-04-14 13:16:54

标签: ruby-on-rails ruby ruby-on-rails-4 testing

在我看来,我有:

<%= link_to image_tag("remove.png", title: "remove"), user, method: :delete, data: { confirm: "This will permanently remove the user" } %>

我正在尝试编写一个没有这样的链接和图像的测试。我试过几个版本,但似乎都没有。我最好的猜测是:

assert_select 'a[href=?]', user_path(user), method: :delete, count: 0
assert_select 'image_path(remove.png)', count: 0

使用第一个assert_select的测试运行但计算1个这样的链接,而我很确定没有这样的链接。我认为它正在计算user_path(用户)确实存在的show方法(而不是delete方法)。

使用第二个assert_select进行测试会产生错误:

unexpected '.' after '["remove"]' (called from block (2 levels) in <class:SiteLayoutTest> at /test/integration/site_layout_test.rb:46)
DEPRECATION WARNING: The assertion was not run because of an invalid css selector.

1 个答案:

答案 0 :(得分:2)

assert_select "a[href=#{user_path(user)}][data-method=delete]", false, "This page must contain no delete user links"

<强>更新

图像的方式相同:

assert_select "img[src=#{image_path('remove.png')}]", false, "This page must contain no remove image"

在css选择器(第一个参数)中,您可以使用任何[attr=value]对html-markup元素(<... attr="value"/>),甚至可以将它们组合[attr1=value1][attr2=value2][...]

更新1

image_path帮助程序方法在测试类中不可用。一种解决方法是(在你的路径中没有另外一张图片&#39;删除&#39;)

assert_select "img[src*=remove]", false, "This page must contain no remove image"

但更好的解决方案是不要依赖路径并使用class属性。 内部视图:

<%= image_tag 'remove.png', class: 'image-remove') %>

在测试方法中:

assert_select "img.image-remove", false, "This page must contain no remove image"