Skip to content

Instantly share code, notes, and snippets.

@akiatoji
Created June 26, 2012 02:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save akiatoji/2992948 to your computer and use it in GitHub Desktop.
Save akiatoji/2992948 to your computer and use it in GitHub Desktop.
rspec let syntax trickery

Seems variables defined by let(){} is lazily instantiated.

This caused problems like so in specs:

let(:estimate){Fabricate.build(:shipping_estimate)}
...
ShippingEstimate.should_receive(:new).and_return(estimate) 
...
# ends up calling methods on uninstantiated (nil) estimate
post :create, valid_params, valid_session  

To avoid this, pre-load let variables where needed:

...
estimate
ShippingEstimate.should_receive(:new).and_return(estimate) 
...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment