Skip to content

let or before(:each)?

May 10, 2013

before early runs code before each test, let lazily runs code when it is first used and it memoizes the value.

`let` is better than `before(:each)` in this case in which I want to access the returned values in several

    let(:devo_1) { FactoryGirl.create(:devotional, :month => 7, :day => 5) }
    let(:devo_2) { FactoryGirl.create(:devotional, :month => 7, :day => 6) }
 
    it 'does something' do
      pp devo_1
    end
    it 'does something else' do
      pp devo_2
    end

   before(:each) do 
      @jul_5_devo = FactoryGirl.create(:devotional, :month => 7, :day => 5) 
      @jul_6_devo = FactoryGirl.create(:devotional, :month => 7, :day => 6) 
      @jul_8_devo = FactoryGirl.create(:devotional, :month => 7, :day => 8) 
      @aug_8_devo = FactoryGirl.create(:devotional, :month => 8, :day => 8) 
   end
    it 'does something' do
      pp devo_1  
    end

    it 'does something' do
      pp devo_2
    end


use before for actions, and use let for dependencies (real or test double).
http://jakegoulding.com/presentations/rspec-structure/#slide-13

Not sure what it exactly means.

Advertisements

From → Uncategorized

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: