LoginSignup
0
0

More than 5 years have passed since last update.

[rspecメモ]チェーンの順番が変更されていないかチェック

Last updated at Posted at 2017-09-08

以下のようなメソッドやスコープの処理のチェーンの順番をチェックしたいときは、

class Teston < ApplicationRecord
  〜省略〜
  scope :hogehoge, -> hoge, limit: 3 do
    joins(:hoge)
      .where(ids: hoge)
      .where.not(id: piyo)
      .order_by_fugafuga
      .fugapiyo
      .limit(4)
  end
  〜省略〜
end

こうする:hugging:

  expect(Teston).to receive_message_chain(
    :joins,
    :where,
    :where, :not,
    :order_by_fugafuga,
    :fugapiyo,
    :limit
  )
  Teston.hogehoge(hoge)

receive_message_chain内で順番にシンボルを並べれば、
並び順をチェックできます。

注意

長いメソッドチェーンはデメテルの法則の反するので、できれば避けましょう。
避けられない場合は入出力のテストに加えて、
今回示したようなチェーンの順番を確認するようなテストもあると、
テストパターンの抜け漏れをカバーできるかもしれません:rocket::waxing_gibbous_moon:

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