Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

assert_select gives misleading message

#4018

When I use assert_select to verify exact number of element matches, I use
assert_select 'a', 5

However the messages (when failing gives)
Expected at least 5 elements matching "a", found 8.

It would be more accurate to change that text to
Expected exactly 5 elements matching "a", found 8.

Jarl

Reported by Jarl Friis · February 21st, 2010 @ 12:37 PM

State: committed
Milestone: 3.0.2
Assigned to: José Valim José Valim
Importance: Low

Activity

  1. David Trasbo
    David Trasbo

    This issue can be duplicated with Rails 2.3.5 and edge Rails:

    2.3.5:

    ➜  code  rails -v
    Rails 2.3.5
    ➜  code  rails assert_select
    ...
    ➜  code  cd assert_select 
    ➜  assert_select  script/generate controller foos
    ...
    ➜  assert_select  rake db:migrate
    (in /Users/dtrasbo/code/assert_select)
    ➜  assert_select  rake test:functionals
    (in /Users/dtrasbo/code/assert_select)
    /Users/dtrasbo/.rvm/rubies/ruby-1.8.7-p249/bin/ruby -I"lib:test" "/Users/dtrasbo/.rvm/gems/ruby-1.8.7-p249@global/gems/rake-0.8.7/lib/rake/rake_test_loader.rb" "test/functional/foos_controller_test.rb" 
    Loaded suite /Users/dtrasbo/.rvm/gems/ruby-1.8.7-p249@global/gems/rake-0.8.7/lib/rake/rake_test_loader
    Started
    F
    Finished in 0.042935 seconds.
    
      1) Failure:
    test_assert_select(FoosControllerTest) [/test/functional/foos_controller_test.rb:7]:
    Expected at least 7 elements matching "a", found 5.
    <false> is not true.
    
    1 tests, 1 assertions, 1 failures, 0 errors
    rake aborted!
    Command failed with status (1): [/Users/dtrasbo/.rvm/rubies/ruby-1.8.7-p249...]
    
    (See full trace by running task with --trace)
    

    Edge:

    ➜  code  ruby rails/bin/rails assert_select --dev
    ...
    ➜  code  cd assert_select 
    ➜  assert_select  ruby ../rails/bin/rails g controller foos index
    ...
    ➜  assert_select  rake db:migrate
    (in /Users/dtrasbo/code/assert_select)
    ➜  assert_select  rake test:functionals
    (in /Users/dtrasbo/code/assert_select)
    Loaded suite /Users/dtrasbo/.rvm/gems/ruby-1.8.7-p249@global/gems/rake-0.8.7/lib/rake/rake_test_loader
    Started
    F
    Finished in 0.107355 seconds.
    
      1) Failure:
    test_assert_select(FoosControllerTest) [/test/functional/foos_controller_test.rb:6]:
    Expected at least 2 elements matching "a", found 1.
    <false> is not true.
    
    1 tests, 1 assertions, 1 failures, 0 errors
    rake aborted!
    Command failed with status (1): [/Users/dtrasbo/.rvm/rubies/ruby-1.8.7-p249...]
    
    (See full trace by running task with --trace)
    

    Both:

    require 'test_helper'
    
    class FoosControllerTest < ActionController::TestCase
      # Replace this with your real tests.
      test "assert_select" do
        get :index
        assert_select 'a', 7
      end
    end
    

    Patch welcome.

    April 15th, 2010 @ 11:11 AM

  2. Lenary
    Lenary

    david, your outputs are confusing. did you really use the same tests for both of them?

    rails 3:

        Expected at least 7 elements matching "a", found 5.
    

    rails 3:

        Expected at least 2 elements matching "a", found 1.
    

    April 15th, 2010 @ 03:06 PM

  3. Rizwan Reza
    Rizwan Reza
    • Tag changed from assert_select, testing to assert_select, bugmash, testing

    Here is the relevant code:

    Expected #{count_description(min, max)} matching "#{selector.to_s}", found #{matches.size}.
    

    I think it's okay for me. ;)

    May 15th, 2010 @ 10:30 AM

  4. PacoGuzman
    PacoGuzman

    In the explanation of the assert_select method we have:

      # === Equality Tests
      #
      # The equality test may be one of the following:
      # * <tt>true</tt> - Assertion is true if at least one element selected.
      # * <tt>false</tt> - Assertion is true if no element selected.
      # * <tt>String/Regexp</tt> - Assertion is true if the text value of at least
      #   one element matches the string or regular expression.
      # * <tt>Integer</tt> - Assertion is true if exactly that number of
      #   elements are selected.
      # * <tt>Range</tt> - 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
    

    So, if we use an Integer like Jarl did I think the error message not correspond to the description of the behaviour of the method.

    I'll work to fix this, What do you think?

    May 15th, 2010 @ 11:36 AM

  5. PacoGuzman
    PacoGuzman

    I've attached a patch

    Now, when use assert_select with false or Integer for the equality test the error message would be more accurate, for example:

    Expected exactly 5 elements matching "a", found 8.

    May 15th, 2010 @ 12:34 PM

  6. Jeff Kreeftmeijer
    Jeff Kreeftmeijer

    +1

    Applies to master cleanly, all tests pass.

    May 15th, 2010 @ 01:44 PM

  7. pleax
    pleax

    +1

    Patch applies cleanly, well tested and all tests pass.

    May 15th, 2010 @ 02:19 PM

  8. Rizwan Reza
    Rizwan Reza
    • Milestone cleared.
    • Tag changed from assert_select, bugmash, testing to assert_select, bugmash, patch, testing
    • State changed from new to verified
    • Assigned user set to Yehuda Katz (wycats)

    May 15th, 2010 @ 03:03 PM

  9. José Valim
    José Valim
    • State changed from verified to committed
    • Assigned user changed from Yehuda Katz (wycats) to José Valim

    Applied.

    May 15th, 2010 @ 04:00 PM

  10. Rizwan Reza
    Rizwan Reza
    • Tag changed from assert_select, bugmash, patch, testing to assert_select, patch, testing

    May 15th, 2010 @ 06:22 PM

  11. Jeremy Kemper
    Jeremy Kemper
    • Milestone set to 3.0.2
    • Importance changed from to Low

    October 15th, 2010 @ 11:01 PM