前回tagを指定することで一部のテストのみ実行する方法を紹介しましたが、tag以外にも文字列の検索機能や行番号指定を利用して特定のテストのみを選択して実行することができます。
その際にはrspecコマンドに–example (もしくは-e)オプションや:(行番号)を使用します。
※RSpecのバージョンはRspec3を想定しています。

–example (もしくは-e)

–exampleのあとにキーワードを指定すると、group(=describe,context), example(=it)のコメント文が検索され、ヒットしたgroupやexampleのテストのみが実行されます。キーワード検索は部分一致なので、コメント文の中の一部の文字を指定すればOKです。

describe 'describe 1' do
  context 'in context 1' do
    it 'is hello' do
      expect('hello').to eq('hello')
    end
    it 'is bye' do
      expect('bye').to eq('bye')
    end
  end
  context 'in context 2' do
    it 'is hello' do
      expect('hello').to eq('hello')
    end
  end
end

‘hello’がコメントに記述されたテストのみを実行するには以下のようにします。

rspec --example 'hello'

‘hello’を含むテストのみ実行されます。

describe 1
  in context 1
    is hello
  in context 2
    is hello

入れ子になったコメントをスペースでつなげて指定することも可能です。
2つある’is hello’のうち、’context 1’内の’is hello’のみを実行する場合は以下のようにします。

rspec -fd --example 'in context 1 is hello'

※結果が見やすいように-fdオプション(–format documentation)を指定しています。

context 1の’is hello’のみ実行されます。

describe 1
  in context 1
    is hello

行番号を指定して実行

指定された行以降のテストのみ実行したり、指定された行間のテストのみ実行することも可能です。
行番号を指定する場合はもちろんファイル名を指定する必要があります。

[spec/simple_spec.rb]

1  describe 'describe 1' do
2    context 'in context 1' do
3      it 'is hello' do
4        expect('hello').to eq('hello')
5      end
6      it 'is bye' do
7        expect('bye').to eq('bye')
8      end
9    end
10   context 'in context 2' do
11     it 'is hello' do
12       expect('hello').to eq('hello')
13     end
14   end
15 end

‘in context2’のみ(10行目以降)を実行する場合は以下のようにします。

rspec -fd spec/simple_spec.rb:10

※結果が見やすいように-fdオプション(–format documentation)を指定しています。

describe 1
  in context 2
    is hello

‘is bye’のみ(6-8行目)を実行する場合は以下のようにします。

rspec -fd spec/simple_spec.rb:6:8

※結果が見やすいように-fdオプション(–format documentation)を指定しています。

describe 1
  in context 1
    is bye

行指定は部分的にかかっていれば、そのテストを実行してくれるので。上の例は

rspec -fd spec/simple_spec.rb:7:8

rspec -fd spec/simple_spec.rb:8:8

でも同じ結果になります。

エラーが出たら即座に終了

若干主旨が違いますが、エラーが出たら即終了させることも可能です。
–fail-fastオプションを指定して実行してください。

rspec --fail-fast