public
Created

Bad error message using rspec's #=~ on hashes

  • Download Gist
output
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
pete@balloon:~/projects/rspec-expectations$ rspec rspec_test.rb
FF
 
Failures:
 
1) Hash #=~ displays confusing error message when hashes are equal
Failure/Error: actual.should =~ actual
expected: {:foo=>"bar"}
got: {:foo=>"bar"} (using =~)
Diff:{:foo=>"bar"}.==({:foo=>"bar"}) returned false even though the diff between {:foo=>"bar"} and {:foo=>"bar"} is empty. Check the implementation of {:foo=>"bar"}.==.
# ./lib/rspec/expectations/fail_with.rb:33:in `fail_with'
# ./lib/rspec/matchers/operator_matcher.rb:47:in `fail_with_message'
# ./lib/rspec/matchers/operator_matcher.rb:69:in `__delegate_operator'
# ./lib/rspec/matchers/operator_matcher.rb:59:in `eval_match'
# ./lib/rspec/matchers/operator_matcher.rb:28:in `block in use_custom_matcher_or_delegate'
# ./rspec_test.rb:5:in `block (2 levels) in <top (required)>'
# /home/pete/.rvm/gems/ruby-1.9.3-p327@rspec-expectations/bundler/gems/rspec-core-e227bb4bf288/lib/rspec/core/example.rb:114:in `instance_eval'
# /home/pete/.rvm/gems/ruby-1.9.3-p327@rspec-expectations/bundler/gems/rspec-core-e227bb4bf288/lib/rspec/core/example.rb:114:in `block in run'
# /home/pete/.rvm/gems/ruby-1.9.3-p327@rspec-expectations/bundler/gems/rspec-core-e227bb4bf288/lib/rspec/core/example.rb:254:in `with_around_each_hooks'
# /home/pete/.rvm/gems/ruby-1.9.3-p327@rspec-expectations/bundler/gems/rspec-core-e227bb4bf288/lib/rspec/core/example.rb:111:in `run'
# /home/pete/.rvm/gems/ruby-1.9.3-p327@rspec-expectations/bundler/gems/rspec-core-e227bb4bf288/lib/rspec/core/example_group.rb:388:in `block in run_examples'
# /home/pete/.rvm/gems/ruby-1.9.3-p327@rspec-expectations/bundler/gems/rspec-core-e227bb4bf288/lib/rspec/core/example_group.rb:384:in `map'
# /home/pete/.rvm/gems/ruby-1.9.3-p327@rspec-expectations/bundler/gems/rspec-core-e227bb4bf288/lib/rspec/core/example_group.rb:384:in `run_examples'
# /home/pete/.rvm/gems/ruby-1.9.3-p327@rspec-expectations/bundler/gems/rspec-core-e227bb4bf288/lib/rspec/core/example_group.rb:369:in `run'
# /home/pete/.rvm/gems/ruby-1.9.3-p327@rspec-expectations/bundler/gems/rspec-core-e227bb4bf288/lib/rspec/core/command_line.rb:28:in `block (2 levels) in run'
# /home/pete/.rvm/gems/ruby-1.9.3-p327@rspec-expectations/bundler/gems/rspec-core-e227bb4bf288/lib/rspec/core/command_line.rb:28:in `map'
# /home/pete/.rvm/gems/ruby-1.9.3-p327@rspec-expectations/bundler/gems/rspec-core-e227bb4bf288/lib/rspec/core/command_line.rb:28:in `block in run'
# /home/pete/.rvm/gems/ruby-1.9.3-p327@rspec-expectations/bundler/gems/rspec-core-e227bb4bf288/lib/rspec/core/reporter.rb:34:in `report'
# /home/pete/.rvm/gems/ruby-1.9.3-p327@rspec-expectations/bundler/gems/rspec-core-e227bb4bf288/lib/rspec/core/command_line.rb:25:in `run'
# /home/pete/.rvm/gems/ruby-1.9.3-p327@rspec-expectations/bundler/gems/rspec-core-e227bb4bf288/lib/rspec/core/runner.rb:80:in `run'
# /home/pete/.rvm/gems/ruby-1.9.3-p327@rspec-expectations/bundler/gems/rspec-core-e227bb4bf288/lib/rspec/core/runner.rb:17:in `block in autorun'
 
2) Hash #=~ displays correct error message when hashes differ
Failure/Error: actual.should =~ expected
expected: {:bar=>"baz"}
got: {:foo=>"bar"} (using =~)
Diff:
@@ -1,2 +1,2 @@
-:bar => "baz"
+:foo => "bar"
# ./lib/rspec/expectations/fail_with.rb:33:in `fail_with'
# ./lib/rspec/matchers/operator_matcher.rb:47:in `fail_with_message'
# ./lib/rspec/matchers/operator_matcher.rb:69:in `__delegate_operator'
# ./lib/rspec/matchers/operator_matcher.rb:59:in `eval_match'
# ./lib/rspec/matchers/operator_matcher.rb:28:in `block in use_custom_matcher_or_delegate'
# ./rspec_test.rb:12:in `block (2 levels) in <top (required)>'
# /home/pete/.rvm/gems/ruby-1.9.3-p327@rspec-expectations/bundler/gems/rspec-core-e227bb4bf288/lib/rspec/core/example.rb:114:in `instance_eval'
# /home/pete/.rvm/gems/ruby-1.9.3-p327@rspec-expectations/bundler/gems/rspec-core-e227bb4bf288/lib/rspec/core/example.rb:114:in `block in run'
# /home/pete/.rvm/gems/ruby-1.9.3-p327@rspec-expectations/bundler/gems/rspec-core-e227bb4bf288/lib/rspec/core/example.rb:254:in `with_around_each_hooks'
# /home/pete/.rvm/gems/ruby-1.9.3-p327@rspec-expectations/bundler/gems/rspec-core-e227bb4bf288/lib/rspec/core/example.rb:111:in `run'
# /home/pete/.rvm/gems/ruby-1.9.3-p327@rspec-expectations/bundler/gems/rspec-core-e227bb4bf288/lib/rspec/core/example_group.rb:388:in `block in run_examples'
# /home/pete/.rvm/gems/ruby-1.9.3-p327@rspec-expectations/bundler/gems/rspec-core-e227bb4bf288/lib/rspec/core/example_group.rb:384:in `map'
# /home/pete/.rvm/gems/ruby-1.9.3-p327@rspec-expectations/bundler/gems/rspec-core-e227bb4bf288/lib/rspec/core/example_group.rb:384:in `run_examples'
# /home/pete/.rvm/gems/ruby-1.9.3-p327@rspec-expectations/bundler/gems/rspec-core-e227bb4bf288/lib/rspec/core/example_group.rb:369:in `run'
# /home/pete/.rvm/gems/ruby-1.9.3-p327@rspec-expectations/bundler/gems/rspec-core-e227bb4bf288/lib/rspec/core/command_line.rb:28:in `block (2 levels) in run'
# /home/pete/.rvm/gems/ruby-1.9.3-p327@rspec-expectations/bundler/gems/rspec-core-e227bb4bf288/lib/rspec/core/command_line.rb:28:in `map'
# /home/pete/.rvm/gems/ruby-1.9.3-p327@rspec-expectations/bundler/gems/rspec-core-e227bb4bf288/lib/rspec/core/command_line.rb:28:in `block in run'
# /home/pete/.rvm/gems/ruby-1.9.3-p327@rspec-expectations/bundler/gems/rspec-core-e227bb4bf288/lib/rspec/core/reporter.rb:34:in `report'
# /home/pete/.rvm/gems/ruby-1.9.3-p327@rspec-expectations/bundler/gems/rspec-core-e227bb4bf288/lib/rspec/core/command_line.rb:25:in `run'
# /home/pete/.rvm/gems/ruby-1.9.3-p327@rspec-expectations/bundler/gems/rspec-core-e227bb4bf288/lib/rspec/core/runner.rb:80:in `run'
# /home/pete/.rvm/gems/ruby-1.9.3-p327@rspec-expectations/bundler/gems/rspec-core-e227bb4bf288/lib/rspec/core/runner.rb:17:in `block in autorun'
 
Finished in 0.00081 seconds
2 examples, 2 failures
 
Failed examples:
 
rspec ./rspec_test.rb:2 # Hash #=~ displays confusing error message when hashes are equal
rspec ./rspec_test.rb:8 # Hash #=~ displays correct error message when hashes differ
rspec_test.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14
describe Hash do
it "#=~_match displays confusing error message when hashes are equal" do
actual = {:foo => 'bar'}
actual.should =~ actual
end
it "#=~ displays correct error message when hashes differ" do
actual = {:foo => 'bar'}
expected = {:bar => 'baz'}
actual.should =~ expected
end
end

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.