Skip to content

Instantly share code, notes, and snippets.

@sephraim
Created March 20, 2024 05:33
Show Gist options
  • Save sephraim/cb626f7d9a9bf238b1bc6e2be8ed54a6 to your computer and use it in GitHub Desktop.
Save sephraim/cb626f7d9a9bf238b1bc6e2be8ed54a6 to your computer and use it in GitHub Desktop.
[Passing an argument to shared examples in RSpec] Here's a VCR example

let is not available in the scope of the shared example group definition, but only in the example (it, specify, scenario) scope.

To pass cassette_name to the shared examples, you can use a method argument instead of let. Here's how you can modify your shared examples and usage:

RSpec.shared_examples 'a vehicle' do |opts|
  use_vcr_cassette(opts[:cassette_name])
end

And then use it like this:

it_behaves_like 'a vehicle', cassette_name: 'models/truck' do
  let(:factory_name) { :truck }
end

In this way, 'models/truck' is passed as an argument to the shared example group and can be used directly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment