[rspec-users] question re: stubbing out validations

Zach Dennis zach.dennis at gmail.com
Mon May 11 21:13:10 EDT 2009

On Mon, May 11, 2009 at 12:05 PM, Barun Singh <barunio at gmail.com> wrote:
> Suppose a "User" has many "widgets", and that we have a method in the User
> class that does something like:
> def update_widgets
>   x = ... code to do some calculations ...
>   if x > 5
>     new_widget = widgets.new
>     new_widget.save!
>   else
>     widgets.first.destroy
>   end
>   widgets.reload
> end
> How would one spec this without having to worry about the validations for
> new_widget?

It seems pretty important that update_widgets is able to produce
actual widgets. I would use actual models here rather than try to
stub/mock something out. After all the whole method is about creating
(or destroying) the widgets. It seems pretty central to the behaviour
of the method. However, if what you shared is the "dumbed" down
version of your code for us on the ML then perhaps there is something
complicated going on that makes it a good candidate to be designed

> Is there any way in the spec to tell it to replace instances of
> save! with instances of save(false) or something along those lines? Using
> message expectations is difficult in this case because the new_widget
> doesn't exist before the method is called, so there's not object that can
> ahead of time expect to receive the save! method.

Are you telling us the full story? In the above code you shared you
don't pass any attributes into "widget.new". So, how does it create
valid widgets in the actual app?

> Thanks..
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users

Zach Dennis
http://www.continuousthinking.com (personal)
http://www.mutuallyhuman.com (hire me)
@zachdennis (twitter)

More information about the rspec-users mailing list