On Jan 22, 2009, at 4:26 pm, Alistair Holt wrote:

> I've just started seriously using Rspec writing the specs for a Merb  
> app I'm working on. I've got some model methods which I'm not sure  
> how to test or what the best way to test them is so I'm asking for  
> any advice on my spec attempts. You can see the code @ 
> https://gist.github.com/ea5a553c12c38c4c6e1b

Hi Alistair

What you will probably find gives you most long-term value in the  
specs is to run everything throught you public API.  That makes the  
specs least brittle.  That means getting some Tag objects out and  
putting them through their paces.

I also suggest creating your own API so that you don't rely on your  
(unspecified) ORM.

Finally, you can take advantage of nested describes to cover different  
situations.

All this gives you something like this:

   describe Thing do

     describe "#tag_list" do
       before(:each) do
         @thing = Thing.gen
       end

       describe "when no Tags have been added" do
         it "should have an empty Tag list" do
           @thing.tags.should be_empty
         end
       end

       describe "when some Tags have been added" do
         before(:each) do
           # Better than @thing.tags << Tag.new(:name => "foo")
           @thing.tag("foo")
           @thing.tag("bar")
           @thing.tag("sexy time")
         end

         it "should return a name sorted array of tags as words" do
           @thing.tag_list.should == ["bar", "foo", "sexy time"]
           # Or maybe...
           # @thing.tag_list.sort.should == ["bar", "foo", "sexy time"]
           # ...if you don't want #tags
         end

         describe "(now let's check persistence is working)" do
           before(:each) do
             @thing_reloaded = Thing.get(@thing.id)
           end

           it "should persist tags" do
             @thing_reloaded.tag_list.should == ["bar", "foo", "sexy  
time"]
           end
         end
       end
     end

   end

Does that give you a good starting point?

HTH

Ashley

-- 
http://www.patchspace.co.uk/
http://aviewfromafar.net/
http://twitter.com/ashleymoran


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"NWRUG" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/nwrug-members?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to