[rspec-users] [RSpec, Rails] POST create action spec and before_filter

Tero Tilus tero at tilus.net
Tue May 19 02:04:23 EDT 2009

2009-05-18 21:40, Florent Florent:
> I have this controller: http://pastie.org/482270
> I wrote this spec: http://pastie.org/482273

There are a few spots which draw my attention.  At spec line 4 you
create a mock Page with #save that always fails (succesfull save
returns true).  Is that intentional?  At line 9 you .and_return a
value from a variable you don't set anywhere.  Moreover the "should
set @context" is imo a spec smell (even though it's in description
string), because it refers to internal state of a PagesController
instance, which you should not be interested in, but the external

> The spec "should set @context" works fine.  But when running the
> spec "should build a new message" I got this error:
> <Page(id: integer, title: string, content: text, created_at:
> datetime, updated_at: datetime) (class)> expected :new with
> ({"title"=>"Introduction", "content"=>"page d'introduction"}) once,
> but received it 0 time

Could that have something to do with the fact that you end up stubbing
Page.new one or two times before setting the expectation on it at line
14?  It shouldn't, but you never know.

> I can not find why Page model does not receive new call, and playing
> around I can have the spec running successfully when I remove
> :create from the before_filter line!

Could your filter be failing and the failure not showin up somehow?
Does "should set @context" _really_ succeed?  Comment the other
example one out and then try again.  Generally you shouldn't expect
your examples (or specs) to run in any particular order.

Tero Tilus ## 050 3635 235 ## http://tero.tilus.net/

More information about the rspec-users mailing list