<br><br><div><span class="gmail_quote">On 7/25/07, <b class="gmail_sendername">Mikel Lindsaar</b> &lt;<a href="mailto:raasdnil@gmail.com">raasdnil@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;">
There would be a different way.<br><br>In the idea of &quot;Only test the code you write&quot; all you really need to<br>spec is that you have set the right association, because activerecord<br>has it&#39;s own tests to ensure the uniq call works.
<br><br>You can do this with a call to reflect_on_association.&nbsp;&nbsp;Unfortunately<br>that call does not return a hash, but an association object.<br><br>I wrote a blog post on how to do this easily with a spec_helper.rb<br>method that adds to_hash to reflect on association.
<br><br><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><br>In the end you end up with a spec that looks like this:<br><br> it &quot;should test reflection details&quot; do<br><br>&nbsp;&nbsp;&nbsp;&nbsp;association_results = {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:macro =&gt; :has_many,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:options =&gt; {:through =&gt; :clipping, :uniq =&gt; true},
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:class_name =&gt; &quot;nil&quot;<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br><br>&nbsp;&nbsp;&nbsp;&nbsp;Book.reflect_on_association(:clip).to_hash.should == association_results<br><br>&nbsp;&nbsp;end<br><br><br>Of course, you can add whatever values you want into the hash.
<br><br>I now have one of these specs at the top of pretty much every model<br>spec.&nbsp;&nbsp;Just set up the hash with all your association rules and then<br>this can be checked and kept track of when you are refactoring or<br>changing code.
<br><br>Hope that helps.<br><br>Regards<br><br>Mikel</blockquote><div><br>
Thanx Mikel.&nbsp; <br>
<br>
I didn&#39;t even think of using a uniq on the has_many.&nbsp; I was using a validates_uniquness_of :scope =&gt; &#39;book_id&#39;&nbsp; <br>
<br>
I will definitley look into this one.<br>
<br>
Thankyou<br>
Daniel<br>
 </div><br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">On 7/25/07, Daniel N &lt;<a href="mailto:has.sox@gmail.com">has.sox@gmail.com</a>
&gt; wrote:<br>&gt;<br>&gt;<br>&gt;<br>&gt; On 7/24/07, Daniel N &lt;<a href="mailto:has.sox@gmail.com">has.sox@gmail.com</a>&gt; wrote:<br>&gt; &gt;<br>&gt; &gt;<br>&gt; &gt;<br>&gt; &gt;<br>&gt; &gt; On 7/24/07, Ashley Moran &lt; 
<a href="mailto:work@ashleymoran.me.uk">work@ashleymoran.me.uk</a>&gt; wrote:<br>&gt; &gt; &gt;<br>&gt; &gt; &gt; On 24 Jul 2007, at 14:07, Daniel N wrote:<br>&gt; &gt; &gt;<br>&gt; &gt; &gt; &gt; Any hints as to how to do this without using fixtures?
<br>&gt; &gt; &gt;<br>&gt; &gt; &gt; I did this literally minutes ago.&nbsp;&nbsp;I don&#39;t use fixtures at all,<br>&gt; &gt; &gt; ever.&nbsp;&nbsp;Instead I put the data in the before block, eg:<br>&gt; &gt; &gt;<br>&gt; &gt; &gt; describe Purchase, &quot;assigned to a Quote that already has a saved
<br>&gt; &gt; &gt; purchase&quot; do<br>&gt; &gt; &gt;&nbsp;&nbsp;&nbsp;&nbsp;before(:each) do<br>&gt; &gt; &gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@dealer = Dealer.create!(:name =&gt;<br>&gt; &gt; &gt; &quot;Dealer1&quot;, :external_identifier =&gt; &quot;D123&quot;)<br>
&gt; &gt; &gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@site = Site.create!(:hostname =&gt; &quot;<a href="http://my.host.name">my.host.name</a>&quot;, :dealer =&gt;<br>&gt; &gt; &gt; @dealer)<br>&gt; &gt; &gt;<br>&gt; &gt; &gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@provider = Provider.create!
(:name =&gt; &quot;Provider1&quot;)<br>&gt; &gt; &gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@product = Product.create!(:name =&gt; &quot;Product1&quot;, :provider =&gt;<br>&gt; &gt; &gt; @provider)<br>&gt; &gt; &gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@applicant = Applicant.create!(:first_name =&gt; &quot;Fred&quot;, :last_name
<br>&gt; &gt; &gt; =&gt; &quot;Flinstone&quot;, :email =&gt; &quot; <a href="mailto:fred@flinstone.com">fred@flinstone.com</a>&quot;, :telephone_home =&gt;<br>&gt; &gt; &gt; &quot;01782 123456&quot;)<br>&gt; &gt; &gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@quote_parameters = 
QuoteParameters.create!(:term =&gt;<br>&gt; &gt; &gt; 36, :applicant =&gt; @applicant, :product =&gt; @product, :site =&gt; @site)<br>&gt; &gt; &gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@quote = InsuranceQuote.create!(:valid_until =&gt;<br>&gt; &gt; &gt; 
1.days.from_now, :quote_parameters =&gt; @quote_parameters, :premium =&gt;<br>&gt; &gt; &gt; 250)<br>&gt; &gt; &gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Purchase.create!(:quote =&gt; @quote, :amount_financed =&gt; 200)<br>&gt; &gt; &gt;&nbsp;&nbsp;&nbsp;&nbsp;end<br>&gt; &gt; &gt;
<br>&gt; &gt; &gt;&nbsp;&nbsp;&nbsp;&nbsp;it &quot;should be invalid&quot; do<br>&gt; &gt; &gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;purchase = Purchase.new(:quote =&gt;<br>&gt; &gt; &gt; @quote, :amount_collected_by_card =&gt; &quot;value&quot;)<br>&gt; &gt; &gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;purchase.should_not
 be_valid<br>&gt; &gt; &gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;purchase.errors.on (:quote_id).should_not be_nil<br>&gt; &gt; &gt;&nbsp;&nbsp;&nbsp;&nbsp;end<br>&gt; &gt; &gt; end<br>&gt; &gt; &gt;<br>&gt; &gt; &gt; Comments welcome on whether this is a good way to do this.&nbsp;&nbsp;(I know
<br>&gt; &gt; &gt; that the above doesn&#39;t test my database unique constraint, which I<br>&gt; &gt; &gt; don&#39;t do in this case come to think of it, but I try to keep DB specs<br>&gt; &gt; &gt; separate, hard as that may be sometimes.)
<br>&gt; &gt; &gt;<br>&gt; &gt; &gt; Ashley<br>&gt; &gt;<br>&gt; &gt;<br>&gt; &gt; That looks like the kind of thing I was initially thinking of, but I<br>&gt; started to think that having all those objects being created in a
<br>&gt; non-related spec could lead to trouble.&nbsp;&nbsp;If I change what makes any of those<br>&gt; objects valid, these supposedly unrelated specs will then break.<br>&gt; &gt;<br>&gt; &gt; I want to try and keep everything seperate, but in this case it seems that
<br>&gt; things are interdependent.<br>&gt; &gt;<br>&gt; &gt; Cheers<br>&gt; &gt; Daniel<br>&gt;<br>&gt; What I&#39;ve ended up doing is the following test<br>&gt;<br>&gt;&nbsp;&nbsp; it &quot;should have a uniq clip_id for a given book&quot; do
<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp; lambda do<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Clipping.create( valid_clipping_attributes )<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Clipping.create( valid_clipping_attributes )<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp; end.should change( Clipping, :count ).by( 1 )<br>&gt;&nbsp;&nbsp; end<br>&gt;<br>
&gt; It hits the database, but the valid_clipping_attributes are mocks that are<br>&gt; used throughout the spec so it&#39;s divorced from the other specs.<br>&gt;<br>&gt; If there are any better ideas I&#39;m open to them.
<br>&gt;<br>&gt; Thanx for the response Ashley.<br>&gt;<br>&gt; Daniel<br>&gt;<br>&gt;<br>&gt;<br>&gt; _______________________________________________<br>&gt; rspec-users mailing list<br>&gt; <a href="mailto:rspec-users@rubyforge.org">
rspec-users@rubyforge.org</a><br>&gt; <a href="http://rubyforge.org/mailman/listinfo/rspec-users">http://rubyforge.org/mailman/listinfo/rspec-users</a><br>&gt;<br>_______________________________________________<br>rspec-users mailing list
<br><a href="mailto:rspec-users@rubyforge.org">rspec-users@rubyforge.org</a><br><a href="http://rubyforge.org/mailman/listinfo/rspec-users">http://rubyforge.org/mailman/listinfo/rspec-users</a><br></blockquote></div><br>