RSpecのMatcher基礎
前回RSpecを使ってみました。
その中で、eq
を使いました。
RSpecには、eq
以外にも多くのMatcherが用意されています。
基本的なMatcherの使用例
a = b
a
とb
が等しい
expect(a).to be eq(b)
a ≠ b
a
とb
が等しくない
expect(a).not_to eq(b)
a < b
a
がb
より小さい
expect(a).to be < b
a >= b
a
がb
以上
expect(a).to be < b
a = 0
a
が0
expect(a).to be_zero
a
が空
expect(a).to be_empty
a
がt
型
expect(a).to be_a(t) expect(a).to be_an(t)
a
はt
のインスタンス
expect(a).to be_instance_of(t)
have_odd_values
やbe_multiple_of(3)
などもある。
このほかにも、様々なMatcherが用意されている。
↓を見てみると、本当にいろんな種類のMatcherがあることがわかる。
RSpec Expectations 3.10 - RSpec Expectations - RSpec - Relish
終わり