LoginSignup
48
29

More than 5 years have passed since last update.

RSpecのshared_examplesとかshared_examples_forとか

Last updated at Posted at 2015-11-08

Ruby初心者である私が、いろいろと奮闘している途中で学んだことその2。

RSpecを書いている時に、共通の内容があるときはshared_examplesを使うとよい。
書き方はこんな感じ。

shared_examples_for 'Some Example' do
  it 'should be something' do
    # Some Tests here
  end
end

describe 'なにかのとき' do
  context '何かの場合' do
    it_behaves_like 'Some Example'
  end
end

書いていてよくわからなかったのが、調べているとこんな風にいろんな書き方があった。
shared_examples
shared_examples_for
shared_context

なんの違いだろうと思って確認したが、あまり情報が見つからず、下記のコード内で発見。
https://github.com/rspec/rspec-core/blob/master/lib/rspec/core/shared_example_group.rb

alias shared_context      shared_examples
alias shared_examples_for shared_examples

単なるshared_examplesメソッドの別名だった。
テストの内容によってどれを使うか決めれば良さそう。

48
29
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
48
29