与assert_select相反?

发布于 2024-12-29 08:33:32 字数 108 浏览 3 评论 0原文

我正在编写一个应用程序,需要检查视图是否不具有某些功能 - 特别是因为该功能必须仅呈现给特定安全组中的用户。我正在寻找与assert_selects相反的内容,以便查看菜单是否呈现。

I am writing an app where it is desirable to check if a view does not have some functionality - in particular because that functionality must be presented only to users in certain security group. I am looking for the opposite of assert_selects in order to see that a menu is not rendered.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

述情 2025-01-05 08:33:32

请查看此处的文档:

http://apidock.com/rails/ActionController/Assertions /SelectorAssertions/assert_select

从文档中:

assert_select 是一个选择元素并使一个或多个相等的断言测试。

以及相等测试部分:

相等性测试可以是以下之一:

true - 如果至少选择了一个元素,则断言为 true。

false - 如果未选择任何元素,则断言为 true。

字符串/正则表达式 - 如果文本值至少为 1,则断言为 true
元素与字符串或正则表达式匹配。

Integer - 如果恰好有该数量的元素,则断言为 true
已选择。

范围 - 如果所选元素的数量符合范围,则断言为 true
范围。

如果没有指定相等性测试,则断言为真,如果至少有一个
选定的元素。

一个简单的例子:

   # Page contains no forms
   assert_select "form", false, "This page must contain no forms"

Take a look at the docs here:

http://apidock.com/rails/ActionController/Assertions/SelectorAssertions/assert_select

From the docs:

assert_select is an assertion that selects elements and makes one or more equality tests.

and from the equality tests sections:

The equality test may be one of the following:

true - Assertion is true if at least one element selected.

false - Assertion is true if no element selected.

String/Regexp - Assertion is true if the text value of at least one
element matches the string or regular expression.

Integer - Assertion is true if exactly that number of elements are
selected.

Range - Assertion is true if the number of selected elements fit the
range.

If no equality test specified, the assertion is true if at least one
element selected.

And a simple example:

   # Page contains no forms
   assert_select "form", false, "This page must contain no forms"
平定天下 2025-01-05 08:33:32

不要忘记您始终可以传入计数并将其设置为零。

assert_select "a", {count: 0, text: "New"}, "This page must contain no anchors that say New"

Don't forget you can always pass in the count, and set that to zero.

assert_select "a", {count: 0, text: "New"}, "This page must contain no anchors that say New"
我一向站在原地 2025-01-05 08:33:32

我遇到这个问题是为了确保在许多按钮中没有“删除”按钮。接受的答案在这种情况下不起作用,因为已经有几个按钮。

assert_select '.button' do |btn|
  btn.each do |b|
    assert_no_match 'Delete', b.to_s
  end
end

不过,我真的更喜欢GantMan的回答!

I had this problem to ensure there WASN'T a 'Delete' button, out of many buttons. The accepted answer would not work in this situation because there are already several buttons.

assert_select '.button' do |btn|
  btn.each do |b|
    assert_no_match 'Delete', b.to_s
  end
end

However, I really like GantMan's answer better!

抱猫软卧 2025-01-05 08:33:32

您可以轻松定义自己的:

module ActionDispatch::Assertions::SelectorAssertions
  def refute_select(*a,&b)
    begin
      assert_select(*a,&b)
    rescue AssertionFailedError
      return
    end
    raise "fail" # there should be a better built-in alternative
  end
end

You can easily define your own:

module ActionDispatch::Assertions::SelectorAssertions
  def refute_select(*a,&b)
    begin
      assert_select(*a,&b)
    rescue AssertionFailedError
      return
    end
    raise "fail" # there should be a better built-in alternative
  end
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文