[rspec-users] Stubbing out required associated models

Matthew Lins mlins at webinforem.com
Wed Nov 14 11:35:31 EST 2007


Hello,

As I'm becoming more and more familiar with mocking/stubbing, I'm going back
to some of my model specs and revaluating how I wrote them.

I have certain models in which validation requires the presence of an
associated model, which itself needs to be valid(validates_presence_of and
validates_associated).

When I wrote the specs, I wrote helpers for the attributes instead of
fixtures, like so:

module UserSpecHelper
  
  def valid_user_attributes
    {
      :email => "test at here.com",
      :password => "tttttt",
      :password_confirmation => "tttttt"
    }
  end
  
end

If the model had a required associated model, I'd also add some valid
attribute helpers to make the associated model pass as well:

module UserSpecHelper
  
  def valid_user_attributes
    {
      :email => "test at here.com",
      :password => "tttttt",
      :password_confirmation => "tttttt"
    }
  end

  def valid_shipping_address_attributes
    {
      :street => "blah street",
      :city => "blah",
      :state => "CA",
      :zip => "54455"
    }
  end  
  
end

Now, I've been thinking, I should probably just stub out the associated
models in order to focus my specs on one model, the model being tested.

Would that be better practice?

Thanks,

Matt Lins




More information about the rspec-users mailing list