<br><br><div><span class="gmail_quote">On 7/25/07, <b class="gmail_sendername">Kyle Hargraves</b> &lt;<a href="mailto:philodespotos@gmail.com">philodespotos@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;">
Daniel N wrote:<br>&gt; On 7/25/07, Mikel Lindsaar &lt;<a href="mailto:raasdnil@gmail.com">raasdnil@gmail.com</a>&gt; wrote:<br>&gt;&gt;<br>&gt;&gt; There would be a different way.<br>&gt;&gt;<br>&gt;&gt; In the idea of &quot;Only test the code you write&quot; all you really need to
<br>&gt;&gt; spec is that you have set the right association, because activerecord<br>&gt;&gt; has it&#39;s own tests to ensure the uniq call works.<br>&gt;&gt;<br>&gt;&gt; You can do this with a call to reflect_on_association.&nbsp;&nbsp;Unfortunately
<br>&gt;&gt; that call does not return a hash, but an association object.<br>&gt;&gt;<br>&gt;&gt; I wrote a blog post on how to do this easily with a spec_helper.rb<br>&gt;&gt; method that adds to_hash to reflect on association.
<br>&gt;&gt;<br>&gt;&gt;<br>&gt;&gt; <a href="http://www.blognow.com.au/q/67540/Reflect_on_association_one_liner_to_check_association_details.html">http://www.blognow.com.au/q/67540/Reflect_on_association_one_liner_to_check_association_details.html
</a><br>&gt;&gt;<br>&gt;&gt;<br>&gt;&gt; In the end you end up with a spec that looks like this:<br>&gt;&gt;<br>&gt;&gt; it &quot;should test reflection details&quot; do<br>&gt;&gt;<br>&gt;&gt;&nbsp;&nbsp;&nbsp;&nbsp; association_results = {
<br>&gt;&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; :macro =&gt; :has_many,<br>&gt;&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; :options =&gt; {:through =&gt; :clipping, :uniq =&gt; true},<br>&gt;&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; :class_name =&gt; &quot;nil&quot;<br>&gt;&gt;&nbsp;&nbsp;&nbsp;&nbsp; }<br>&gt;&gt;<br>&gt;&gt;&nbsp;&nbsp;&nbsp;&nbsp; Book.reflect_on_association
(:clip).to_hash.should ==<br>&gt;&gt; association_results<br>&gt;&gt;<br>&gt;&gt;&nbsp;&nbsp; end<br>&gt;&gt;<br>&gt;&gt;<br>&gt;&gt; Of course, you can add whatever values you want into the hash.<br>&gt;&gt;<br>&gt;&gt; I now have one of these specs at the top of pretty much every model
<br>&gt;&gt; spec.&nbsp;&nbsp;Just set up the hash with all your association rules and then<br>&gt;&gt; this can be checked and kept track of when you are refactoring or<br>&gt;&gt; changing code.<br>&gt;&gt;<br>&gt;&gt; Hope that helps.
<br>&gt;&gt;<br>&gt;&gt; Regards<br>&gt;&gt;<br>&gt;&gt; Mikel<br>&gt;<br>&gt;<br>&gt; Thanx Mikel.<br>&gt;<br>&gt; I didn&#39;t even think of using a uniq on the has_many.&nbsp;&nbsp;I was using a<br>&gt; validates_uniquness_of :scope =&gt; &#39;book_id&#39;
<br>&gt;<br>&gt; I will definitley look into this one.<br>&gt;<br>&gt; Thankyou<br>&gt; Daniel<br><br>However you decide to go about solving it, the idiom you hit upon is<br>what, to me, actually describes the behaviour; if the book already has a
<br>clip once, you can&#39;t add it again:<br><br>&nbsp;&nbsp; lambda { add_twice }.should change(...).by(1)<br><br>It states very clearly what results you wish to see.<br><br>Going through reflect_on_association feels too much like specifying the
<br>use of a particular implementation, rather than the desired outcome.<br><br>Kyle</blockquote><div><br>
<br>
It looks much better when you write it like that though ;)<br>
</div><br></div><br>