Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

rspec testing a controller post changing my params from symbols to strings and breaking my tests

In my controller spec I am doing this:

it "should create new message" do
  Client.should_receive(:create).with({:title => 'Mr'})
  post 'create' , :client => {:title => "Mr" }
end

... and in my controller I am doing ...

def create
  client = Client.create(params[:client])
end

However this is failing with the following error message :

  expected: ({:title=>"Mr"})
       got: ({"title"=>"Mr"})

I'm wondering why this is happening and how to get it to work

like image 587
ssmithstone Avatar asked Feb 02 '10 10:02

ssmithstone


2 Answers

It's because you are passing a symbol and not a string. This should fix it :

it "should create new message" do
  Client.should_receive(:create).with({:title => 'Mr'})
  post 'create' , :client => {"title" => "Mr" }
end

Here's a blogpost about it: "Understanding Ruby Symbols"

like image 87
marcgg Avatar answered Sep 28 '22 05:09

marcgg


@ssmithone you could use ActiveSupport::HashWithIndifferentAccess to pass params as symbols instead of strings. This should work:

it "should create new message" do
  Client.should_receive(:create).with({:title => 'Mr'}.with_indifferent_access)
  post 'create', :client => {:title => "Mr"}
end
like image 41
olivoil Avatar answered Sep 28 '22 05:09

olivoil