[rspec-users] let vs ivar w/ before block
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)
> 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
> rspec-users mailing list
> rspec-users at rubyforge.org
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)'.
More information about the rspec-users