Today I Learned

hashrocket A Hashrocket project

RSpec let!

RSpec’s let method comes in two varieties.

let can be used to define a memoized helper method.

let(:current_user) {  FactoryGirl.create :user }

But let is lazily-evaluated, meaning it isn’t evaluated until the first time the method is invoked. The value returned will be cached across multiple calls in the same example.

let! forces the method to be evaluated before each example in a before hook.

let!(:current_user) {  FactoryGirl.create :user, active_range: (Time.now)...(Time.now + 1.second) }

You might use `let! when you need a method to be invoked before each example, or when measuring precise time-dependent behavior where lazy evaluation could produce inconsistent results. Source

See More #testing TILs