Stubbing out required associated models

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 => “[email protected]”,
: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 => “[email protected]”,
: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 L.

On Nov 14, 2007, at 4:08 PM, sinclair bain wrote:

Matthew,

If your intent is to test the behaviour of your model(s), then yes
mocking and
stubbing are a good [the] way to go.

Except, of course, that you are no longer really validating the
associated models - only your mocks.

Scott

Matthew,
If your intent is to test the behaviour of your model(s), then yes
mocking
and
stubbing are a good [the] way to go.

Cheers!
sinclair

of course, you do both. you need to unit test in isolation, as well as
integration test the stack.

But, that should be okay, since my intent is not to test the associated
models. I have a separate spec file for those models.