[rspec-devel] [ rspec-Feature Requests-6940 ] Expectation failure message for AR::Base.new is misleading

noreply at rubyforge.org noreply at rubyforge.org
Tue Nov 28 18:51:43 EST 2006


Feature Requests item #6940, was opened at 2006-11-28 17:04
You can respond by visiting: 
http://rubyforge.org/tracker/?func=detail&atid=3152&aid=6940&group_id=797

Category: None
Group: None
Status: Open
Priority: 3
Submitted By: Pat Maddox (pergesu)
Assigned to: Nobody (None)
Summary: Expectation failure message for AR::Base.new is misleading

Initial Comment:
Edge Rails, 0.7.3

If you have the expectation
Person.should_receive(:new).with(:name => "BJ").and_return(@mock_person)

then a seemingly valid call will fail.  The call would be something like
get :new, :person => { :name => "BJ" }

and the controller code would be
Person.new params[:user]

What actually gets passed into new is { "name" => "BJ } - I'm not sure if Rails is stringifying the hash keys or if RSpec is.  Anyway, changing the spec to
Person.should_receive(:new).with("name" => "BJ").and_return(@mock_person)

works fine (notice that :name became "name").  The real problem is that when you run the first spec, it fails with 

Person expected :new with [{:name=>"BJ"}] once, but received it 0 times

That's misleading, because :new was called, it just didn't have the right arguments.  It should say something like

Person expected :new with [{:name=>"BJ"}] once, but received it with [{"name"=>"BJ"}]

I made a simple test case (not Rails) to demonstrate this, and to my surprise got
Foo received unexpected message :foo with [1]
(it should have received "1")

So normally RSpec shows reasonable output, and this behavior is isolated only to Rails.  It's saying that a method wasn't called, when it fact it was, but with the wrong arguments.

Also, it'd be good to track down whether it's Rails or RSpec that is changing the key from :name to "name".  If it's Rails doing that, then we should probably have RSpec do it under the hood as well, to match behavior.  It's kind of weird to pass in { :name => "BJ" } and then be told that it wasn't received...and then find out that { "name" => "BJ" } was received instead.

----------------------------------------------------------------------

>Comment By: Aslak Hellesøy (aslak_hellesoy)
Date: 2006-11-28 18:51

Message:
Two things seem to be needed here.

First, better error messages like you suggest.

Second, we should define ActiveRecord::Base.should_receive to coerce hashes into HashWithIndifferentAccess before passing it up to RSpec core.


----------------------------------------------------------------------

You can respond by visiting: 
http://rubyforge.org/tracker/?func=detail&atid=3152&aid=6940&group_id=797


More information about the rspec-devel mailing list