Help us understand the problem. What is going on with this article?

RSpecのshared_examplesとかshared_examples_forとか

More than 3 years have passed since last update.

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メソッドの別名だった。
テストの内容によってどれを使うか決めれば良さそう。

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away