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

David Chelimsky dchelimsky at gmail.com
Fri Sep 30 16:05:37 EDT 2011


On Sep 30, 2011, at 2:48 PM, Patrick J. Collins wrote:

> Am I correct in understanding that the point of using let is that it can
> minimize database calls due to memoization?
> 
> describe "#something" do
> 
>  let(:something) = Something.groovy_scope
> 
>  it "does x" do
>    something.first.x.should have_coolness
>  end
> 
>  it "does y" do
> 	 something.first.y.should_not have_coolness
>  end
> 
> end
> 
> vs.
> 
> describe "#something" do
> 
>  before :each do
>    @something = Something.groovy_scope
>  end
> 
>  it "does x" do
> 	 @something.first.x.should_not have_coolness
>  end
> 
>  it "does y" do
> 	 @something.first.y.should_not have_coolness
>  end
> 
> end
> 
> ...
> 
> Is that the proper usage?

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

let(:thing) { Thing.new }

it "does something" do
  thing.blah
  thing.whatever
  thing.yet_again
end

In that case each reference to thing returns the same object.

Make sense?


More information about the rspec-users mailing list