Forum: RSpec Hash / params causing errors

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Fbd9cb107fe7c941333d6a3488691989?d=identicon&s=25 Ramon Tayag (ramontayag)
on 2008-11-07 03:59
(Received via mailing list)
Hey everyone,

I'm trying to spec a controller and I can't seem to get a test to pass
because rspec seems to see the difference between params I expect and
params that I pass.  However, this comes from the same params
variable.  What should I do to get this to work?

Spec::Mocks::MockExpectationError in 'UsersController handling POST
/users should create a new user from params'
Mock 'Class' expected :new with ({:email=>"user@email.com"}) but
received it with ({"email"=>"user@email.com"})

Code is here: http://pastie.org/309263

Thanks!
Ramon Tayag
5d38ab152e1e3e219512a9859fcd93af?d=identicon&s=25 David Chelimsky (Guest)
on 2008-11-07 04:23
(Received via mailing list)
On Thu, Nov 6, 2008 at 9:53 PM, Ramon Tayag <ramon.tayag@gmail.com>
wrote:
> received it with ({"email"=>"user@email.com"})
expected {:email=>"user@email.com"}
got {"email"=>"user@email.com"}

Rails is converting what's really passed (with a Symbol key) to a
String key. It'll work if you expect the String instead.
Fbd9cb107fe7c941333d6a3488691989?d=identicon&s=25 Ramon Tayag (ramontayag)
on 2008-11-07 04:32
(Received via mailing list)
Thanks for you reply.  With what you said I ended up doing this

  before do
    @user = mock_model(User, :to_param => "1", :login => "loginator",
:email => "user@email.com", :save! => true)
    User.stub!(:new).and_return(@user)
    @params = { :login => "loginator", :email => "user@email.com" }
  end

...

  it "should create a new user from params" do
    User.should_receive(:new).with({"login" => "loginator", "email" =>
"user@email.com"}).and_return(@user)
    do_post
  end
Fbd9cb107fe7c941333d6a3488691989?d=identicon&s=25 Ramon Tayag (ramontayag)
on 2008-11-07 04:43
(Received via mailing list)
A bit dryer:

...
@params = { "login" => "loginator", "email" => "user@email.com" }
...
User.should_receive(:new).with(@params).and_return(@user)
...

Ramon Tayag
This topic is locked and can not be replied to.