Not sure why let is not doing the same thing as traditional test

Here is my test which passes.

before do
@page = Factory(:page)
@note = Factory(:note, :page => @page, :title => ‘super cool’)
end
it ‘has size 1’ do
@page.notes.size.should == 1
end

I read rspec book and wanted to use let. Here is my implementation.
And it fails

 let(:page) { Factory(:page) }
  let(:note) { Factory(:note, :page => page, :title => 'super

cool’) }
it ‘has size 1’ do
page.notes.size.should == 1
end

In the log I noticed that when I switched to using let then no note
record is being created.

Here is relevant gem file

group :development, :test do
gem ‘mongrel’
gem ‘capistrano’, “>= 2.5.19”
gem ‘capistrano-ext’

gem “factory_girl_rails”

gem “database_cleaner”
gem “shoulda”

gem “rspec-rails”, “>= 2.0.1”
gem “cucumber-rails”, “>= 0.3.2”
gem “capybara”, “= 0.4.0”
gem “launchy”
gem “redgreen”
gem “faker”
gem “mongrel”
end

Is let lazy? It seems since I am not using note the record is not
being created.

The book said that output is memoized. Nothing more than that.

I’m under the weather so I won’t be able to give you a thorough
answer.

#let - The block is executed when you call it.
#let! - The block is “wrapped” in a before(:each) filter.

So, you want to use #let!

That worked. Thanks a lot.

Wish you speedy recovery Justin.

take care.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs