LoginSignup
0
0

More than 3 years have passed since last update.

RSpec で ある要素のみが入った配列かどうかを確認したいときは contain_exactly を使う

Posted at

概要

a,b,c のリソースがあるときに、a,c のみが順番は関係なく返されることをテストしたいときには、contain_exactly が便利です。

コード

let!(:user_a) { create(:user, name: 'a') }
let!(:user_b) { create(:user, name: 'b') }
let!(:user_c) { create(:user, name: 'c') }

it 'returns ac users do
  expect(User.hoge).to contain_exactly(user_a, user_c)
end

順番もテストするときは素直に eq matcher で配列を使う。

it 'returns ac users do
  expect(User.hoge).to eq [user_a, user_c]
end

参考:
contain_exactly matcher
https://relishapp.com/rspec/rspec-expectations/v/3-9/docs/built-in-matchers/contain-exactly-matcher

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