[rspec-users] let vs ivar w/ before block
ash.moran at patchspace.co.uk
Sat Oct 1 06:30:40 EDT 2011
On 1 Oct 2011, at 00:36, Srushti Ambekallu wrote:
> On 30/09/11 6:24 PM, Patrick J. Collins wrote:
>> So regarding objects persisting over multiple examples-- I was told repeatedly
>> by experienced RSpec peeps to not use before(:all)…
> 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)'.
I'll second that. It's better to feel the pain of the multiple database hits and refactor it out in the code (can you separate more of the logic and persistence?), than try to optimise the test run by introducing shared state. In the long run, the former will give you many faster test runs through better design, where as the latter will give you a few faster tests now at the risk of - as Srushti points out - stepping on a landmine later.
More information about the rspec-users