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

Srushti Ambekallu srushti at ambekallu.com
Fri Sep 30 19:36:44 EDT 2011


On 30/09/11 6:24 PM, Patrick J. Collins wrote:
>> 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)
> end
>
> it "has an item" do
>    @user.user_item.should_not be_nil
> end
>
> it "rocks the house" do
>    @user.user_item.should respond_to(:rocks_the_house)
> end
>
> ..etc...
>
> 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
> http://collinatorstudios.com
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users
>
I don't know if object creation (including the corresponding database 
calls) are really expensive enough that I would worry about optimising 
that (unless you're getting it anyway, without losing anything in the 
process, like with 'let'). Occasionally, for some reason I'll end up 
using a 'before(:all)' for one isolated scenario, but inevitably a few 
months down the line, I'll end up wasting a couple of hours on a weird 
failing test, until I look closely enough to notice the ":all".

So, unless I'm doing something that's prohibitively expensive (I haven't 
found anything that would qualify in any of my projects) I'm not likely 
to create anything in a 'before(:all)'.

Srushti
http://c42.in


More information about the rspec-users mailing list