rspec-core icon indicating copy to clipboard operation
rspec-core copied to clipboard

Execute all specs including the given shared examples

Open Startouf opened this issue 7 years ago • 1 comments

Subject of the issue

Ability to run all specs reusing a shared example directly from the command line

Given the specs

# spec/examples/cuteness_examples.rb
shared_examples_for 'a cute pet' do 
   it('is small enough') { expect(subject.cute?).to eq(true) }
   it('makes a cute sound') { expect(subject.sound.cute?).to eq(true) }
end

# spec/models/octocat_spec.rb
describe 'Octocat'
  it_behaves_like 'a cute pet'
end

# spec/models/the_doge_spec.rb
describe 'The Doge'
  it_behaves_like 'a cute pet'
end

Expect the following commands to run (and only run) Octocat & The Doge's cuteness specs

rspec --run-shared-examples spec/examples/cuteness_examples.rb
# =>
Octocat
  is small enough
  makes a cute sound
The Doge
  is small enough
  makes a cute sound

An advanced version of this feature would even support specifying the line of shared example you wish to see specs of

rspec --run-shared-examples spec/examples/cuteness_examples.rb:21
# =>
Octocat
  is small enough
The Doge
  is small enough
 

rspec --run-shared-examples spec/examples/cuteness_examples.rb:42
# =>
Octocat
  makes a cute sound
The Doge
  makes a cute sound

It would be very useful for people breaking their tests into multiple files with shared examples instead of having to either figure out the devil string, or copy it from failed examples (octocat_spec.rb[1:1:2:5:1])

Startouf avatar Sep 12 '18 18:09 Startouf

This can already be accomplished after a sort (running a set of predetermined specs) through tagging, if you tag the shared examples you can filter to run just those specs through the cli.

JonRowe avatar Sep 12 '18 22:09 JonRowe

Closing due to inactivity during the migration to the monorepo, and there is a workaround.

JonRowe avatar Nov 27 '24 19:11 JonRowe