RSpec - マッチャ一覧
参照は下記。本記事にないマッチャはいずれかを参照すれば見つかる。
公式Doc
Built in matchers - RSpec Expectations - RSpec - Relish
File: README — Documentation for rspec-expectations (3.8.2)
@jnshitoさんの記事
== を検証する eq
expect(actual).to eq(expected)
比較を検証する be >
expect(actual).to be > expected expect(actual).to be >= expected expect(actual).to be <= expected expect(actual).to be < expected
正規表現、文字列との一致を検証する match
expect(actual).to match(/expression/)
true、falseを検証する be_truthy
expect(actual).to be_truthy expect(actual).to be true expect(actual).to be_falsy expect(actual).to be false expect(actual).to be_nil expect(actual).to_not be_nil
配列の要素の同値性を検証する contain_exactly
配列要素の順番は問わない。match_array はエイリアスメソッド。
expect([1, 2, 3]).to contain_exactly(2, 3, 1) expect([:a, :c, :b]).not_to contain_exactly(:a, :c ) expect([1, 2, 3]).to match_array [2, 3, 1] expect([:a, :c, :b]).not_to match_array [:a, :c]
配列に指定した要素を含んでいるかを検証する include
include?を持つオブジェクトに使えるため、arrayだけでなくstringやhashなどに対しても使える。
# Array expect([1, 2]).to include(1) expect([1, 2]).to include(1, 2) expect([1, 2]).not_to include(17) # String expect("a string").to include("a") expect("a string").to include("str") expect("a string").to include("str", "g") expect("a string").not_to include("foo") # Hash expect(:a => 1, :b => 2).to include(:a) expect(:a => 1, :b => 2).to include(:a, :b) expect(:a => 1, :b => 2).to include(:a => 1) expect(:a => 1, :b => 2).to include(:b => 2, :a => 1) expect(:a => 1, :b => 2).not_to include(:c)