This project is archived and is in readonly mode.

#4018 ✓committed
Jarl Friis

assert_select gives misleading message

Reported by Jarl Friis | February 21st, 2010 @ 12:37 PM | in 3.0.2

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

Comments and changes to this ticket

  • David Trasbo

    David Trasbo April 15th, 2010 @ 11:11 AM

    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.

  • Lenary

    Lenary April 15th, 2010 @ 03:06 PM

    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.
    
  • Rizwan Reza

    Rizwan Reza May 15th, 2010 @ 10:30 AM

    • 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. ;)

  • PacoGuzman

    PacoGuzman May 15th, 2010 @ 11:36 AM

    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?

  • PacoGuzman

    PacoGuzman May 15th, 2010 @ 12:34 PM

    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.

  • Jeff Kreeftmeijer

    Jeff Kreeftmeijer May 15th, 2010 @ 01:44 PM

    +1

    Applies to master cleanly, all tests pass.

  • pleax

    pleax May 15th, 2010 @ 02:19 PM

    +1

    Patch applies cleanly, well tested and all tests pass.

  • Rizwan Reza

    Rizwan Reza May 15th, 2010 @ 03:03 PM

    • 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)”
  • José Valim

    José Valim May 15th, 2010 @ 04:00 PM

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

    Applied.

  • Rizwan Reza

    Rizwan Reza May 15th, 2010 @ 06:22 PM

    • Tag changed from assert_select, bugmash, patch, testing to assert_select, patch, testing
  • Jeremy Kemper

    Jeremy Kemper October 15th, 2010 @ 11:01 PM

    • Milestone set to 3.0.2
    • Importance changed from “” to “Low”

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

Attachments

Pages