[rspec-users] question re: stubbing out validations
matt at mattwynne.net
Mon May 11 13:48:21 EDT 2009
On 11 May 2009, at 17:05, Barun Singh 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
> How would one spec this without having to worry about the
> validations for new_widget? 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.
If you have a user object in your specs, you could do this:
user.widgets.stub!(:new).and_return(mock(Widget, :save! => nil))
but you're getting into some ugly territory mixing mocks with calls to
the database (e.g. widgets.reload).
What does your spec look like?
More information about the rspec-users