TaMaMhyu さんが書いておられますが shared_example
でいけますね。
RSpec の入門とその一歩先へ、第2イテレーション ~RSpec 3バージョン~ - Qiita
の例を引用すると
describe MessageFilter, 'with argument "foo"' do
subject { MessageFilter.new('foo') }
it { is_expected.to be_detect('hello from foo') }
it { is_expected.not_to be_detect('hello, world') }
end
describe MessageFilter, 'with argument "foo","bar"' do
subject { MessageFilter.new('foo', 'bar') }
it { is_expected.to be_detect('hello from bar') }
it { is_expected.to be_detect('hello from foo') }
it { is_expected.not_to be_detect('hello, world') }
end
では
it { is_expected.to be_detect('hello from foo') }
it { is_expected.not_to be_detect('hello, world') }
が重複しています(というかコピペしただけです)。
これを
shared_examples 'MessageFilter with argument "foo"' do
it { is_expected.to be_detect('hello from foo') }
it { is_expected.not_to be_detect('hello, world!') }
end
describe MessageFilter, 'with argument "foo"' do
subject { MessageFilter.new('foo') }
it_behaves_like 'MessageFilter with argument "foo"'
end
describe MessageFilter, 'with argument "foo","bar"' do
subject { MessageFilter.new('foo', 'bar') }
it { is_expected.to be_detect('hello from bar') }
it_behaves_like 'MessageFilter with argument "foo"'
end
のように shared_examples
で括り出します。
括り出した部分は it_behaves_like
で指定します。
it_behaves_like
の引数は十分に説明的で、先にある shared_examples
の中身を見にいかなくても済むことが望ましいと考えています。以上が、私が shared_examples
でかなり長い名前をつけた理由です。
とあるように、 shared_examples
ではできるだけ分かりやすい名前を付けるべき。
RSpecの入門とその一歩先へ ~RSpec 3バージョン~ - Qiita の記事を一通り(3つに分かれています)読まれることを強くお勧めします。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。