[rspec-users] Rspec-2 around/before(:all) hooks

David Chelimsky dchelimsky at gmail.com
Wed Nov 3 00:37:35 EDT 2010


On Nov 2, 2010, at 1:54 PM, Mack T. wrote:

> Rspec-2 around/before(:all) hooks
> 
> I'm trying to create several model instances before all of our tests are
> run.  I'm having trouble defining the behavior in my Rspec.configure
> block.  I've tried before(:all) and around(:all), but to no avail.
> 
> before(:all) successfully creates the models, but it appears that
> everything in the block passed is rolled back before the test runs:
> 
> # spec_helper.rb
> config.before(:all) do
>  @person = Person.create(:name => 'not here!')
> end
> 
> # person_spec.rb
> describe Person
>  it do
>    p Person.where(:short_name => 'not here!').first #=> nil
>    p @person #=> <Person id: 1050608284, name: "not here!">
>    @person.reload #=> Error: Couldn't find Person with ID=1050608284
>  end
> end
> 
> It seems that around(:all) is not supported, either.  Is that correct?
> This code in the block passed to configure results in a nil.<< error:
> 
> # spec_helper.rb
> config.around(:all) do
>  puts "hai"
>  yield
>  puts "bai"
> end
> 
> I'm running rspec-2.0.1 and rails-3.0.0.  Is there a preferred way to do
> this that I'm missing?  Thanks in advance!

A few things:

1. I can't reproduce what you're describing. I get the same person object for all three lines in the example (adding a 'p' to the last line). Not sure why that would be.

2. around(:all) is not supported (needs a better error message), but before(:all) should work.

3. What you're trying to do is a bit unusual. By default, each example is run in a transaction which is rolled back at the end of the example. This keeps all of the examples isolated from each other, which is a very desirable outcome.  If you use before(:all), the created object will remain in the database, so it's up to you to clean it up yourself instead of relying on ActiveRecord to do it for you.

Also, before(:all) (or before(:each)) in the configure block in spec_helper adds that to every example group. i.e. it's the same as declaring that block in each group. If you're looking to do this once per run, use before(:suite).

HTH,
David


More information about the rspec-users mailing list