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
    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? 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?

Matt Wynne
http://blog.mattwynne.net
http://www.songkick.com

_______________________________________________
rspec-users mailing list
rspec-users@rubyforge.org
http://rubyforge.org/mailman/listinfo/rspec-users

Reply via email to