Association existence probably isn't all that helpful, but once you
start to use some of the more arcane options then it really, really
helps to have tests around them. I've been burned before by
associations that weren't really working the way I expected them to
(some of the more arcane flags in has_many :through, for example), so
now as soon as I get more complex I start covering with tests. TATFT
and all that.

I thought the suite was a good starting point--better to be more
conservative. :)

My one comment--and I think popular opinion has swung against me
here--is that I don't love the extensive use of the setup block. I
vastly prefer

  Artist.new.releases.should be_empty

to

  setup { @kanye = Artist.new }

 @kanye.releases.should be_empty

The closer the definition is to the assertion, the better.

One last suggestion, not really related to your suite - I don't know
what FactoryGirl does by default these days, but I prefer initialize
or stub to create if I can, and only use create if the test really
relies on the existence of a record. Keeps the suite faster.

Brian

On Sat, Sep 10, 2011 at 6:08 AM, Ben Hoskings <[email protected]> wrote:
> I'd argue that none of these tests are necessary.
>
> You're testing that the many-to-many relationship you've set up between 
> Artist and Release works, but that's already covered in Rails' test suite. 
> You should only test up to the boundary between your app and rails -- i.e. 
> that you've correctly defined the relationship.
>
> If you include the 'shoulda-matchers' gem, you can just write this:
>
>    describe Artist do
>      describe "associations" do
>        it { should have_and_belong_to_many(:releases) }
>      end
>    end
>
>    describe Release do
>      describe "associations" do
>        it { should have_and_belong_to_many(:artists) }
>      end
>    end
>
> Personally, I don't like those specs either, because I think the information 
> they encode is just a duplication of what's already written in the model.
>
> My stance is this: those associations are there for a reason: you want to 
> somehow relate artists and releases, and do operations on them in tandem, or 
> from one to the other. If you directly unit test (ideally, test-drive) those 
> operations throughly, then the associations are implicitly tested.
>
> Or, more to the point: your app doesn't directly need the associations. You 
> want to achieve certain functionality, and the association is an 
> implementation detail that happens to be required for a bunch of other, 
> direct, unit specs to pass.
>
> - Ben
>
>
>
> On 10/09/2011, at 3:53 PM, Stuart Liston wrote:
>
>> Hi ladies and gents
>>
>> I joined this group a few week ago and keep an eye on it but am a little too
>> 'green' to help out a lot of the time. Instead, my first mail will be to
>> look for advice!
>>
>> I'm looking to test some model (HABTM) relationships in a small application
>> I'm building, and I'm keen to show some more experienced developers how I'm
>> going about it so that I don't start to get into bad habits.
>>
>> So with that in mind, does anyone fancy having a quick gander over the
>> following gist and telling me if I'm approaching the problem in a reasonable
>> way? My specs *are* green, it's more a 'best practice'  thing...
>>
>> https://gist.github.com/1207931
>>
>> (The assumption would be that I have rspec and factorygirl set up correctly)
>>
>> Thanks for your time in advance!
>> Stu
>>
>> --
>> You received this message because you are subscribed to the Google Groups 
>> "Ruby or Rails Oceania" group.
>> To post to this group, send email to [email protected].
>> To unsubscribe from this group, send email to 
>> [email protected].
>> For more options, visit this group at 
>> http://groups.google.com/group/rails-oceania?hl=en.
>>
>
> --
> You received this message because you are subscribed to the Google Groups 
> "Ruby or Rails Oceania" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to 
> [email protected].
> For more options, visit this group at 
> http://groups.google.com/group/rails-oceania?hl=en.
>
>

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
or Rails Oceania" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/rails-oceania?hl=en.

Reply via email to