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

James Hughes hughes.james at gmail.com
Mon Feb 19 19:13:47 EST 2007


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?

thanks,
James

>  James Hughes wrote:
>
> Hi,
>
> I have this setup block:
>
>  setup do
>  session[:login] = 'jhughes'
>  @user = mock("user")
>  User.stub!(:find).and_return(@user)
>  @params = {:cn => "Bilbo Baggins",
>  :telephoneNumber => "416-277-4418",
>  :mail => "bilbo at baggins.com"}
>  end
>
> And then this spec:
>
>  specify "should update and save the attributes for user" do
> @user.should_receive(:update_attributes).with(@params).and_return(true)
>  post :update, :id => "jhughes", :user => @params
>  end
>
> This fails like this:
>
> 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"})
>
> Puzzling, especially since inspecting the @params variable just before
> the should_receive shows the expected hash.
>
> What am I doing wrong?
>
> James


More information about the rspec-users mailing list