RSpecのMatcher基礎

f:id:utouto97:20210704221944p:plain

前回RSpecを使ってみました。

utouto97.hatenablog.com

その中で、eq を使いました。
RSpecには、eq以外にも多くのMatcherが用意されています。

基本的なMatcherの使用例

a = b abが等しい

expect(a).to be eq(b)

a ≠ b abが等しくない

expect(a).not_to eq(b)

a < b abより小さい

expect(a).to be < b

a >= b ab以上

expect(a).to be < b

a = 0 aが0

expect(a).to be_zero

aが空

expect(a).to be_empty

at

expect(a).to be_a(t)
expect(a).to be_an(t)

atインスタンス

expect(a).to be_instance_of(t)

have_odd_valuesbe_multiple_of(3)などもある。
このほかにも、様々なMatcherが用意されている。

↓を見てみると、本当にいろんな種類のMatcherがあることがわかる。
RSpec Expectations 3.10 - RSpec Expectations - RSpec - Relish

終わり