Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>let() is supposed to be scoped to the example blocks and unusable elsewhere. You don't actually use let() as parameters. The reason it does not work with it_behaves_like as a parameter has to do with how let() gets defined. Each example group in Rspec defines a custom class. let() defines an instance method in that class. However, when you call it_behaves_like in that custom class, it is calling at the class level rather than from within an instance.</p> <p>I've used let() like this:</p> <pre><code>shared_examples_for 'any connection' do it 'should have valid connection' do connection.valid? end end describe Connection do let(:connection) { Connection.new(settings) } let(:settings) { { :blah =&gt; :foo } } it_behaves_like 'any connection' end </code></pre> <p>I've done something similar to bcobb's answer, though I rarely use shared_examples:</p> <pre><code>module SpecHelpers module Connection extend ActiveSupport::Concern included do let(:connection) { raise "You must override 'connection'" } end module ClassMethods def expects_valid_connection it "should be a valid connection" do connection.should be_valid end end end end end describe Connection do include SpecHelpers::Connection let(:connection) { Connection.new } expects_valid_connection end </code></pre> <p>The definition of those shared examples are more verbose than using shared examples. I guess I find "it_behave_like" being more awkward than extending Rspec directly.</p> <p>Obviously, you can add arguments to .expects_valid_connections</p> <p>I wrote this to help a friend's rspec class: <a href="http://ruby-lambda.blogspot.com/2011/02/agile-rspec-with-let.html">http://ruby-lambda.blogspot.com/2011/02/agile-rspec-with-let.html</a> ...</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload