[rspec-users] let vs ivar w/ before block

Patrick J. Collins patrick at collinatorstudios.com
Fri Sep 30 19:24:58 EDT 2011

> Proper usage, sure, but the memoization is only within each example - not
> across examples. That way you can do this:

So regarding objects persisting over multiple examples--  I was told repeatedly
by experienced RSpec peeps to not use before(:all)...

But in a case like:

before(:each) do
  @user = create_user
  create_user_item(:user => @user)

it "has an item" do
  @user.user_item.should_not be_nil

it "rocks the house" do
  @user.user_item.should respond_to(:rocks_the_house)


It seems like this is an instance where before :all, really would shine because
it would not require records to be repeatedly created...

Do you guys feel like before(:all) is just bad because of the possibility of a
method call in one example changing the state and therefore breaking future
examples and not having it be clear as to why.. ?

Patrick J. Collins

More information about the rspec-users mailing list