Use include matcher obediently when checking hash with RSpec

Overview

This article is about using include matcher instead of eq matcher when you want to check hash with rspec.

code

If you have a hash of {hoge:'fuga'} and you try to check it with eq,

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

You can't write like, because {} is interpreted as a block. So to use eq

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

You have to put the hash in a variable, like.

It may not be necessary to write an article, but

This is a hassle, so use include.

it do
  expect(subject).to include(hoge: 'fuga')
end

Recommended Posts

Use include matcher obediently when checking hash with RSpec
Use webmock with Rspec