<br><br><div><span class="gmail_quote">On 7/6/07, <b class="gmail_sendername">David Chelimsky</b> &lt;<a href="mailto:dchelimsky@gmail.com">dchelimsky@gmail.com</a>&gt; 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 &lt;<a href="mailto:has.sox@gmail.com">has.sox@gmail.com</a>&gt; wrote:<br>&gt; Hi,<br>&gt;<br>&gt;&nbsp;&nbsp;I&#39;m very new to rspec, so if this is not the right forum please let me<br>&gt; know.<br>&gt;<br>
&gt;&nbsp;&nbsp;I&#39;m starting to spec my models first in an existing rails app, porting from<br>&gt; a mix of Test::Unit, and simply_bdd amongst others.<br>&gt;<br>&gt;&nbsp;&nbsp;I&#39;m at the point where I want to test that certain associations are
<br>&gt; present.&nbsp;&nbsp;What I&#39;m not sure of is should I test the association or the<br>&gt; method and return object.<br>&gt;<br>&gt;&nbsp;&nbsp;That is, if I wanted to test a has_many should I:<br>&gt;<br>&gt;&nbsp;&nbsp;Confirm the methods exist, and that the return an array etc
<br>&gt;<br>&gt;&nbsp;&nbsp;OR<br>&gt;<br>&gt;&nbsp;&nbsp;Check that the model has the named has_many association through it&#39;s<br>&gt; reflections.<br>&gt;<br>&gt;&nbsp;&nbsp;On one hand the second one looks like it will be a bit more robust, since
<br>&gt; if there is a has_many relationship, then all the associated methods may be<br>&gt; used througout the app.&nbsp;&nbsp;This would put testing in the one place.<br>&gt;<br>&gt;&nbsp;&nbsp;On the other hand, this would be really testing the implementation of the
<br>&gt; model rather than it&#39;s behaviour.&nbsp;&nbsp; The behaviour is to call<br>&gt; @article.comments and have an array of comments returned.<br>&gt;<br>&gt;&nbsp;&nbsp;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&#39;d say try it<br>both ways and report back on experiences.<br><br>David<br><br>&gt;&nbsp;&nbsp;Cheers<br>&gt;&nbsp;&nbsp;Daniel<br>&gt;</blockquote><div><br>I went with the second way, testing the association through reflections.&nbsp; 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.&nbsp; Which you are then free to use throughout your app.&nbsp; 
<br><br>If you go the other way, you are not in fact testing if a model &quot;has_many&quot; since this implies that all has_many methods will be included, not just returning and setting an Array.<br><br>If your interested I&#39;ve put up the module that I am using to provide these and a couple of other methods on pastie.&nbsp; This is my first go so please don&#39;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>