[rspec-users] Interesting shared behaviour side-effect

Ashley Moran work at ashleymoran.me.uk
Sun Oct 21 08:37:06 EDT 2007


On Oct 21, 2007, at 11:01 am, Wincent Colaiuta wrote:

> And note that it's running the shared behaviours in this order:
>
>    - 'one facet'
>    - 'some other facet'
>    - 'some other facet'
>    - 'one facet'


On a similar note, AGES ago, I promised to submit a patch for  
something I ripped out of the rspec-ext gem, which lets you do this:


   describe "something" do
     facet "one facet" do
       it "should do something"
       it "and a bit more"
     end
     facet "another facet" do
       it "should do something else"
     end
   end

to output something like:

    something
    - one facet: should do something
    - one facet: and a bit more
    - another facet: should do something else


I found it invaluable in long controller specs that have several  
specs for loading, saving, validation etc.  It also helps you  
logically group related specs together in a spec.  I started trying  
to update the patch yesterday, but got stuck because the trunk has  
moved on.  Before I spend too long on it, I have questions:

* is this still useful to anyone here?

* is this good style? I am wondering if instead of

   describe Cat do
     before(:each) do
       @cat = Cat.new
     end
     facet "prod" do
       it "should miaow when sent :prod"
       it "should scratch when sent :prod"
     end
     facet "eat_food" do
       it "should eat food when sent :eat_food"
       it "should sleep after eating food when sent :eat_food"
     end
   end

you should write

   describe Cat, ".prod" do
     before(:each) do
       @cat = Cat.new
     end
     it "should miaow when sent :prod" do
       # ...
       @cat.prod
     end
     it "should scratch when sent :prod"
   end

   describe Cat, ".eat_food" do
     before(:each) do
       @cat = Cat.new
     end
     it "should eat food when sent :eat_food"
     it "should sleep after eating food when sent :eat_food"
   end

ie shift the method call into the "given".  Personally I prefer the  
first way, as I like to formulate everything as a GWT unless I really  
can't.  But every time I look at RSpec (example) code it seems like  
the recommended way of structuring specs changes.

Ashley



-- 
blog @ http://aviewfromafar.net/
linked-in @ http://www.linkedin.com/in/ashleymoran
currently @ home




More information about the rspec-users mailing list