<br><br><div><span class="gmail_quote">On 7/25/07, <b class="gmail_sendername">Mikel Lindsaar</b> <<a href="mailto:raasdnil@gmail.com">raasdnil@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;">
There would be a different way.<br><br>In the idea of "Only test the code you write" all you really need to<br>spec is that you have set the right association, because activerecord<br>has it's own tests to ensure the uniq call works.
<br><br>You can do this with a call to reflect_on_association. 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 "should test reflection details" do<br><br> association_results = {<br> :macro => :has_many,<br> :options => {:through => :clipping, :uniq => true},
<br> :class_name => "nil"<br> }<br><br> Book.reflect_on_association(:clip).to_hash.should == association_results<br><br> 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. 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. <br>
<br>
I didn't even think of using a uniq on the has_many. I was using a validates_uniquness_of :scope => 'book_id' <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 <<a href="mailto:has.sox@gmail.com">has.sox@gmail.com</a>
> wrote:<br>><br>><br>><br>> On 7/24/07, Daniel N <<a href="mailto:has.sox@gmail.com">has.sox@gmail.com</a>> wrote:<br>> ><br>> ><br>> ><br>> ><br>> > On 7/24/07, Ashley Moran <
<a href="mailto:work@ashleymoran.me.uk">work@ashleymoran.me.uk</a>> wrote:<br>> > ><br>> > > On 24 Jul 2007, at 14:07, Daniel N wrote:<br>> > ><br>> > > > Any hints as to how to do this without using fixtures?
<br>> > ><br>> > > I did this literally minutes ago. I don't use fixtures at all,<br>> > > ever. Instead I put the data in the before block, eg:<br>> > ><br>> > > describe Purchase, "assigned to a Quote that already has a saved
<br>> > > purchase" do<br>> > > before(:each) do<br>> > > @dealer = Dealer.create!(:name =><br>> > > "Dealer1", :external_identifier => "D123")<br>
> > > @site = Site.create!(:hostname => "<a href="http://my.host.name">my.host.name</a>", :dealer =><br>> > > @dealer)<br>> > ><br>> > > @provider = Provider.create!
(:name => "Provider1")<br>> > > @product = Product.create!(:name => "Product1", :provider =><br>> > > @provider)<br>> > > @applicant = Applicant.create!(:first_name => "Fred", :last_name
<br>> > > => "Flinstone", :email => " <a href="mailto:fred@flinstone.com">fred@flinstone.com</a>", :telephone_home =><br>> > > "01782 123456")<br>> > > @quote_parameters =
QuoteParameters.create!(:term =><br>> > > 36, :applicant => @applicant, :product => @product, :site => @site)<br>> > > @quote = InsuranceQuote.create!(:valid_until =><br>> > >
1.days.from_now, :quote_parameters => @quote_parameters, :premium =><br>> > > 250)<br>> > > Purchase.create!(:quote => @quote, :amount_financed => 200)<br>> > > end<br>> > >
<br>> > > it "should be invalid" do<br>> > > purchase = Purchase.new(:quote =><br>> > > @quote, :amount_collected_by_card => "value")<br>> > > purchase.should_not
be_valid<br>> > > purchase.errors.on (:quote_id).should_not be_nil<br>> > > end<br>> > > end<br>> > ><br>> > > Comments welcome on whether this is a good way to do this. (I know
<br>> > > that the above doesn't test my database unique constraint, which I<br>> > > don't do in this case come to think of it, but I try to keep DB specs<br>> > > separate, hard as that may be sometimes.)
<br>> > ><br>> > > Ashley<br>> ><br>> ><br>> > That looks like the kind of thing I was initially thinking of, but I<br>> started to think that having all those objects being created in a
<br>> non-related spec could lead to trouble. If I change what makes any of those<br>> objects valid, these supposedly unrelated specs will then break.<br>> ><br>> > I want to try and keep everything seperate, but in this case it seems that
<br>> things are interdependent.<br>> ><br>> > Cheers<br>> > Daniel<br>><br>> What I've ended up doing is the following test<br>><br>> it "should have a uniq clip_id for a given book" do
<br>> lambda do<br>> Clipping.create( valid_clipping_attributes )<br>> Clipping.create( valid_clipping_attributes )<br>> end.should change( Clipping, :count ).by( 1 )<br>> end<br>><br>
> It hits the database, but the valid_clipping_attributes are mocks that are<br>> used throughout the spec so it's divorced from the other specs.<br>><br>> If there are any better ideas I'm open to them.
<br>><br>> Thanx for the response Ashley.<br>><br>> Daniel<br>><br>><br>><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>><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>