The opposite of assert_select?

Ruby on-RailsFunctional Testing

Ruby on-Rails Problem Overview


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.

Ruby on-Rails Solutions


Solution 1 - Ruby on-Rails

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"

Solution 2 - Ruby on-Rails

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"

Solution 3 - Ruby on-Rails

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!

Solution 4 - Ruby on-Rails

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionVictor PudeyevView Question on Stackoverflow
Solution 1 - Ruby on-RailsjordanpgView Answer on Stackoverflow
Solution 2 - Ruby on-RailsGant LabordeView Answer on Stackoverflow
Solution 3 - Ruby on-RailsChloeView Answer on Stackoverflow
Solution 4 - Ruby on-RailsAlex DView Answer on Stackoverflow