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

Patrick J. Collins patrick at collinatorstudios.com
Fri Sep 30 16:31:40 EDT 2011


> 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?

Hmm.. now I am confused...

What is the difference between:

describe "Foo" do

  let(:foo) { Foo.new }

  it "is tubular" do
    foo.bar
    foo.baz
  end

  it "is gnarly" do
    foo.gnarl
    foo.wurd_up
  end

end

vs.

describe "Foo" do

  before :each do
    @foo = Foo.new
  end

  it "seems just as tubular as the foo w/ let" do
    @foo.bar
    @foo.baz
  end

  it "seems just as gnarly as the foo w/ let" do
    foo.gnarl
    foo.wurd_up
  end

end

I am not seeing any difference...?


Patrick J. Collins
http://collinatorstudios.com


More information about the rspec-users mailing list