[rspec-users] Odd parameter munging with with()

David Chelimsky dchelimsky at gmail.com
Tue Feb 20 06:03:02 EST 2007


On 2/19/07, James Hughes <hughes.james at gmail.com> wrote:
> On 2/19/07, David Chelimsky <dchelimsky at gmail.com> wrote:
> > On 2/19/07, James Hughes <hughes.james at gmail.com> wrote:
> > > On 2/15/07, Jerry West <jerry.west at ntlworld.com> wrote:
> > > >
> > > >  It might be worth filing an RFE on this as it's counter-intuitive and
> > > > likely to bite others as well
> > >
> > > Hello again,
> > >
> > > In trying to create some simple code that would reproduce this without
> > > all the rails dependencies for an RFE, I came up with this:
> > >
> > > class Test
> > >   def Test.amethod params
> > >     true
> > >   end
> > > end
> > > context "passing a hash to #with" do
> > >   specify "should pass a hash to #with" do
> > >     @params = { :param1 => "foo",
> > >       :param2 => "bar"}
> > >     Test.should_receive(:amethod).with(@params).and_return(true)
> > >     Test.amethod @params
> > >   end
> > > end
> > >
> > > I thought this was conceptually the same as my failing spec from the
> > > original post, but obviously it's not, as it passes. Can anyone
> > > explain what the difference is?
> >
> > The difference that I see is that the new example passes @params
> > directly to Test.amethod, whereas the earlier example uses "post
> > :update". So I'm guessing that the problem is related specifically to
> > how "post :update" packages up the arguments to pass to the model.
> >
> > WDYT?
>
> If the failure message is to be believed, it's #with that is packaging
> up the arguments in an odd way.
> Take a look at the failure message again:
>
> > > > Mock 'user' expected :update_attributes with ([:telephoneNumber,
> > > > "416-277-4418"], [:mail, "bilbo at baggins.com"], [:cn, "Bilbo Baggins"])
> > > > but received it with ({"cn"=>"Bilbo Baggins",
> > > > "telephoneNumber"=>"416-277-4418",
> > > > "mail"=>"bilbo at baggins.com"})
>
> See what I mean? (I just checked, and post was doing one modification
> to @params: it was changing the keys from symbols to strings. However,
> changing the @params keys to strings in the spec still produces the
> same error as above, so that's not the problem.) For final proof,
> here's the output from a couple of print statements, one in the spec
> just before post(), and one in the controller action, just before
> calling update_attributes:
>
> ............in spec
> {"cn"=>"Bilbo Baggins", "telephoneNumber"=>"416-277-4418",
> "mail"=>"bilbo at baggins.com"}
> in controller
> {"cn"=>"Bilbo Baggins", "telephoneNumber"=>"416-277-4418",
> "mail"=>"bilbo at baggins.com"}
>
> So, I still say my app code and my reduction look conceptually the
> same, but I'm stumped as to why they don't behave the same way (not to
> mention I don't understand the need for #with to mangle it's args as
> it does).

RSpec is doing the comparison correctly. The Rails version is
comparing a Hash with stringified keys w/ a Hash w/ symbols as keys,
whereas the other  version is comparing the exact same Hash (w/ symbol
keys) with itself.

The bug is really in the message. I'm fixing the message so it'll
correctly show you the expected Hash, but IMO RSpec shouldn't be
messing around w/ keys.

WDYT?

David

>
> James
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users
>


More information about the rspec-users mailing list