[rspec-users] Order guarantees of let

David Chelimsky dchelimsky at gmail.com
Mon Aug 9 11:21:05 EDT 2010


On Aug 9, 2010, at 10:06 AM, Rick DeNatale wrote:

> On Mon, Aug 9, 2010 at 8:49 AM, David Chelimsky <dchelimsky at gmail.com> wrote:
>> 
>> On Aug 9, 2010, at 7:38 AM, Ashley Moran wrote:
>> 
>>> Hi
>>> 
>>> I was just about to replace a `before` block along the lines of:
>>> 
>>>  before(:each) do
>>>    @cti_b_id = service.create(name: "Item-B")
>>>    @cti_z_id = service.create(name: "Z-Item")
>>>    @cti_a_id = service.create(name: "Item-A")
>>>    # ...
>>>  end
>>> 
>>> with
>>> 
>>>   let!(:cti_b_id) { ... }
>>>   let!(:cti_z_id) { ... }
>>>   let!(:cti_a_id) { ... }
>>> 
>>> But then I wondered - since the spec depends on the order they are created in (it proves ordering is independent of creation order) - is the run order of `let!` guaranteed?  I imagine they run in the order I expect (ie top to bottom), but I wondered if that was an explicitly stated property of RSpec?
>> 
>> Yes, eval'd in order. No, not explicitly stated, but I think it should be. Want to submit a patch with a spec for this?
> 
> But,
> 
> First of all, what's let! as opposed to let, I can't seem to find it
> via google or the latest draft of the book.

http://rdoc.info/projects/rspec/rspec-core - search for let and let!

> 
> Second, unless let! is a new method which not only defines the
> memoized method but invokes it

That's what it is. Sort of. It gets invoked by an implicit before hook which is added - those get eval'd in the order in which they get defined.

> , then the order of evaluation will
> depend on the order the generated let methods are invoked in the
> example won't it?
> 
> 
> -- 
> Rick DeNatale
> 
> Blog: http://talklikeaduck.denhaven2.com/
> Github: http://github.com/rubyredrick
> Twitter: @RickDeNatale
> WWR: http://www.workingwithrails.com/person/9021-rick-denatale
> LinkedIn: http://www.linkedin.com/in/rickdenatale
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users



More information about the rspec-users mailing list