0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

RSpecのテストコードを整理する:describe→context→it の使い分け

Posted at

describe の使い方
describeブロックを二重に使用するパターン
context の使い方
it の使い方
before テストの整理とリファクタリング
簡易例

describe の使い方

describe ブロックは、主題を明確にするために使用します。
クラスやメソッドなど、テストしたい対象の単位でグループ化します。
#full_name#はインスタンスメソッドを識別している

ruby
describe User do
  # Userクラスのテストを記述
end

describe '#full_name' do
  # full_nameメソッドのテストを記述
end

describeブロックを二重に使用するパターン

ruby
describe Person do
  describe '#name' do
    context '名前が設定されている場合' do
      it '名前を正しく返すこと' do
        person = Person.new(name: '山田太郎')
        expect(person.name).to eq('山田太郎')
      end
    end

    context '名前が設定されていない場合' do
      it 'nilを返すこと' do
        person = Person.new(name: nil)
        expect(person.name).to be_nil
      end
    end
  end

  describe '#age' do
    context '年齢が設定されている場合' do
      it '年齢を正しく返すこと' do
        person = Person.new(age: 30)
        expect(person.age).to eq(30)
      end
    end

    context '年齢が設定されていない場合' do
      it 'nilを返すこと' do
        person = Person.new(age: nil)
        expect(person.age).to be_nil
      end
    end
  end
end

最上位の describePerson クラス
nameage)は describe の属性で、各々に焦点を当てグループ化しています。

contextは以下の章で記述している通りですが、
更に詳細に区分して、各属性が設定
されているか?
されていないか?
という条件を基にテストグループ化しています。

context の使い方

context ブロックは、条件や状況を基にテストをグループ化する際に使用します。
結果が返ってくる場合、返ってこない場合という2つの状態を使いこなします。

ruby
describe '#full_name' do
  context 'ユーザーにミドルネームがある場合のテスト' do
    #テストコード
  end

  context 'ユーザーにミドルネームがない場合のテスト' do
    #テストコード
  end
end

it の使い方

it ブロックは、具体的な期待値を記述します。
これはテストの最小単位で、一つの it ブロックには一つの振る舞いや結果が期待されます。

ruby
describe '#full_name' do
  context 'ユーザーにミドルネームがある場合のテスト' do
    it 'ミドルネームを含む' do
      user = User.new(first_name: 'John', middle_name: 'H.', last_name: 'Doe')
      expect(user.full_name).to eq('John H. Doe')
    end
  end
end

before テストの整理とリファクタリング

DRY原則を守るために、before ブロックを利用して共通のセットアップを行います。
テスト毎に繰り返し必要になる前提条件や処理を一箇所にまとめ、コードの重複を防ぎます。

ruby
describe '#full_name' do
  before do
    @user = User.new(first_name: 'John', last_name: 'Doe')
  end

  it '連結されたフルネームを返す' do
    expect(@user.full_name).to eq('John Doe')
  end
end

簡易例

ruby
describe '数値の加算' do
  context '両方の数値が正の場合' do
    it '合計を正しく計算する' do
      expect(1 + 2).to eq(3)
    end
  end

  context '一方の数値が負の場合' do
    it '正しい合計を返す' do
      expect(1 + (-1)).to eq(0)
    end
  end

  context '両方の数値が負の場合' do
    it '合計が負の数になる' do
      expect(-1 + (-1)).to eq(-2)
    end
  end
end

describe '数値の加算': 数値加算機能全体をテストする範囲を示しています。
context '両方の数値が正の場合': 両方の数値が正の値である場合の動作を検証します。
context '一方の数値が負の場合': 一方の数値が負である場合の結果を検証します。
context '両方の数値が負の場合': 両方の数値が負の場合の合計をテストします。
itブロック: 各itブロックでは、特定の期待される結果を詳細にテストしています。

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?