LoginSignup
10
8

More than 5 years have passed since last update.

Shared example内の特定のexampleを実行したい時

Posted at

例えば以下の様な状況だとします。

user_spec.rb
describe User do
  it_behaves_like 'person'

  des...
end
shared_examples_for_person.rb
shared_examples_for 'person' do
  describe 'instance methods' do
    subject { create(:"#{described_class.class_name.underscore}") }

    describe '#dead?' do
      context 'when dead' do
        before { subject.alive = false }

        it 'returns true' do
          expect(subject.dead?).to be_true
        end
      end
    end
  end
end

そしてuser_spec.rbを実行したら、shared_examples_for_person.rbのテストだけが失敗して、次からはその失敗したテストケースだけを実行したくなったりする時がありますよね。

落ちたテストがshared_examplesじゃなかったら$ rspec ./spec/models/user_spec.rb:200てな具合に個別で実行出来るんですが、
shared_exampleは行数の指定が出来ないようです。(https://github.com/rspec/rspec-core/issues/793)

そんな時は

$ rspec -e "when dead" ./spec/modesl/user_spec.rb

と実行すると"when dead" context内のテストを実行してくれるようです。(https://github.com/rspec/rspec-core/issues/835#issuecomment-15111557)

10
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
10
8