[rspec-users] [Rspec] How do you nest before(:all) or after(:all) blocks?

Scott Taylor scott at railsnewbie.com
Wed Feb 4 10:30:09 EST 2009


John Kolokotronis wrote:
> Hi all,
>
> I'm new to Rspec but loving it so far and looking to use it as a
> replacement for a Test::Unit framework I have which drives a web app
> via Watir. So far, things have worked very well with Rspec but I can't
> get my head around how before/after(:all) blocks would work in nested
> groups, or even if what I am doing is possible.
>
> What I want to do is something like this:
>
> describe "This is a set of tests: " do
> 	before(:all) do
> 		puts "this should only appear once"
> 	end
>
> 	describe "Test 1" do
> 		puts "test 1"
> 	end
>
> 	describe "Test 2" do
> 		puts "test 2"
> 	end
> end
>
> Expecting the before(:all) block to run only once and see something
> like:
>
> - this should only appear once
> - test 1
> - test 2
>
> but what I am getting is:
>
> - this should only appear once
> - test 1
> - this should only appear once
> - test 2
>
> So my question is, how can you add before/after(:all) blocks that run
> once and only once for for all examples, if all the examples are
> nested in one main describe block? Is that something that's even
> possible? I'd appreciate any pointers anyone may have. Thanks!
>
>   

Why do you want before(:all)?  Generally it's use is disregarded, just 
as global variables are considered harmful, not because they can't be 
used well, but because 99% of the times they aren't.

Scott



More information about the rspec-users mailing list