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 3 years have passed since last update.

RSpec で hash の確認をするときは素直に include matcher を使おう

Posted at

概要

rspec で hash を確認したいときに、eq matcher ではなく、include matcher を素直に使おうという記事です。

コード

{ hoge: 'fuga' }という hash があるときに、これを eq で確認しようとすると、

it do
  expect(subject).to eq { hoge: 'fuga' }
end

のようには書けません、{}がブロックと解釈されるからです。
なので eq を使うためには

it do
  expected_hash = { hoge: 'fuga' }
  expect(subject).to eq expected_hash
end

のように、hash を変数に入れなければなりません。

当たり前で記事にするまでもないかもしれませんが、

これでは面倒なのでinclude を使いましょう。

it do
  expect(subject).to include(hoge: 'fuga')
end
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?