<br><br><div><span class="gmail_quote">On 7/6/07, <b class="gmail_sendername">David Chelimsky</b> <<a href="mailto:dchelimsky@gmail.com">dchelimsky@gmail.com</a>> wrote:</span><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
On 7/3/07, Daniel N <<a href="mailto:has.sox@gmail.com">has.sox@gmail.com</a>> wrote:<br>> Hi,<br>><br>> I'm very new to rspec, so if this is not the right forum please let me<br>> know.<br>><br>
> I'm starting to spec my models first in an existing rails app, porting from<br>> a mix of Test::Unit, and simply_bdd amongst others.<br>><br>> I'm at the point where I want to test that certain associations are
<br>> present. What I'm not sure of is should I test the association or the<br>> method and return object.<br>><br>> That is, if I wanted to test a has_many should I:<br>><br>> Confirm the methods exist, and that the return an array etc
<br>><br>> OR<br>><br>> Check that the model has the named has_many association through it's<br>> reflections.<br>><br>> On one hand the second one looks like it will be a bit more robust, since
<br>> if there is a has_many relationship, then all the associated methods may be<br>> used througout the app. This would put testing in the one place.<br>><br>> On the other hand, this would be really testing the implementation of the
<br>> model rather than it's behaviour. The behaviour is to call<br>> @article.comments and have an array of comments returned.<br>><br>> Any thoughts?<br><br>I think the jury is still out on this one. Both approaches present
<br>problems, and no better approaches have been proposed. I'd say try it<br>both ways and report back on experiences.<br><br>David<br><br>> Cheers<br>> Daniel<br>></blockquote><div><br>I went with the second way, testing the association through reflections. The reason I did this is that by testing that there is a has_* or belongs_to you are really testing the availablity of all the assoicated methods. Which you are then free to use throughout your app.
<br><br>If you go the other way, you are not in fact testing if a model "has_many" since this implies that all has_many methods will be included, not just returning and setting an Array.<br><br>If your interested I've put up the module that I am using to provide these and a couple of other methods on pastie. This is my first go so please don't expect anything spectacular.
<br><br><a href="http://pastie.caboo.se/76462">http://pastie.caboo.se/76462</a><br></div><br></div>Cheers<br>Daniel<br>