[rspec-users] describe blocks without before(:all) / after(:all)

David Chelimsky dchelimsky at gmail.com
Tue Mar 30 00:23:39 EDT 2010


On Mar 29, 2010, at 10:36 PM, Jimmy Soho wrote:

> Hi All,
> 
> We have many specs like the pattern given below.
> 
> The problem is that it's slow, because each inner context block will execute the before(:all) in the outer describe block.
> 
> Is there a way to setup an inner context block that does not execute the before(:all) and after(:all), but does execute the before(:each) and after(:each) ?  

Not in rspec-1, but that's how rspec-2 works by default.

I don't want to change this in rspec-1 because I don't plan to release any more major 1.x releases, and this would risk being a compatibility-breaking change.

HTH,
David


> Based on some playing around (removing inner context blocks) we think this would speed up our tests by a factor of 10.
> 
> 
> Cheers,
> Jimmy
> 
> -----
> 
> describe Project do
>   before(:all) do
>     # statements that set up an object graph, relatively slow
>   end
>   
>   after(:all) do
>     # tear down object graph
>   end
> 
>   it { should ... }
>   it { should ... }
>   it { should ... }
>   it { should ... }
>   
>   context "target calculations" do
>     before(:each) do
>       # non-expensive statements, just setting up for example the subject, Time.zone, locale, etc. 
>     end
>     
>     it { should ... }
>     it { should ... }
>     it { should ... }
>   end
>   
>   context "duration calculations" do
>     before(:each) do
>       # non-expensive statements
>     end
>     
>     it { should ... }
>     it { should ... }
>     it { should ... }
>   end
> 
>   context ...
>   context ...
>   [ etc. ]
> 
> end
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users



More information about the rspec-users mailing list