[rspec-users] Specting controller post

J. B. Rainsberger jbrainsberger at gmail.com
Tue Feb 2 07:07:56 EST 2010

On Tue, Feb 2, 2010 at 04:24, Stephen Smithstone <ssmithstone at me.com> wrote:

> A bit stuck here being new to ruby and rspec

Welcome. I hope you like the experience. Good news: we've seen what
you've done a thousand times and so it's quite easy to explain.

> it "should create new client" do
>     // creates me a hash of generated values using Foregey
>     client = Client.plan
>     // specify that I should be creating a new client with the hash values
> from client
>     Client.should_receive(:new).with(client)
>     //post to the server
>     post 'create' , :client => client
> end
> in my controller
> def create
>     client = Client.new(params[:client])
> end
> now this is failing with
> Spec::Mocks::MockExpectationError: <Client(id: integer, created_at:
> datetime, updated_at: datetime, title: string, lastname: string, firstname:
> string, email: string) (class)> received :new with unexpected arguments
>   expected: ({:title=>"Mr", :firstname=>"Ernest", :lastname=>"Burke",
> :email=>"rgarrett at blogtag.info"})
>        got: ({"title"=>"Mr", "lastname"=>"Burke", "firstname"=>"Ernest",
> "email"=>"rgarrett at blogtag.info"})
> is there a way to specify to use symbols instead of the strings as keys, as
> I dont want to be doing
> :client => { "title" => client.title , ............ }
> as the client model may change over time

Try Client.should_receive(:new).with(client.attributes), because the
#attributes method turns a model into a Hash of symbol to value. It
uses string keys instead of symbol keys, but that shouldn't hurt you.

Good luck.
J. B. (Joe) Rainsberger :: http://www.jbrains.ca ::
Diaspar Software Services :: http://www.diasparsoftware.com
Author, JUnit Recipes
2005 Gordon Pask Award for contribution to Agile practice :: Agile
2010: Learn. Practice. Explore.

More information about the rspec-users mailing list