Built-in matchers
同値判定
expect(actual).to eq(expected) # passes if actual == expected
expect(actual).to eql(expected) # passes if actual.eql?(expected)
expect(actual).not_to eql(not_expected) # passes if not(actual.eql?(expected))
同一オブジェクト判定
expect(actual).to be(expected) # passes if actual.equal?(expected)
expect(actual).to equal(expected) # passes if actual.equal?(expected)
比較
expect(actual).to be > expected
expect(actual).to be >= expected
expect(actual).to be <= expected
expect(actual).to be < expected
expect(actual).to be_within(delta).of(expected)
正規表現
expect(actual).to match(/expression/)
型
expect(actual).to be_an_instance_of(expected) # passes if actual.class == expected
expect(actual).to be_a(expected) # passes if actual.kind_of?(expected)
expect(actual).to be_an(expected) # an alias for be_a
expect(actual).to be_a_kind_of(expected) # another alias
真偽
expect(actual).to be_truthy # passes if actual is truthy (not nil or false)
expect(actual).to be true # passes if actual == true
expect(actual).to be_falsy # passes if actual is falsy (nil or false)
expect(actual).to be false # passes if actual == false
expect(actual).to be_nil # passes if actual is nil
expect(actual).to_not be_nil # passes if actual is not nil
例外
expect { ... }.to raise_error
expect { ... }.to raise_error(ErrorClass)
expect { ... }.to raise_error("message")
expect { ... }.to raise_error(ErrorClass, "message")
throw
expect { ... }.to throw_symbol
expect { ... }.to throw_symbol(:symbol)
expect { ... }.to throw_symbol(:symbol, 'value')
yield
expect { |b| 5.tap(&b) }.to yield_control # passes regardless of yielded args
expect { |b| yield_if_true(true, &b) }.to yield_with_no_args # passes only if no args are yielded
expect { |b| 5.tap(&b) }.to yield_with_args(5)
expect { |b| 5.tap(&b) }.to yield_with_args(Integer)
expect { |b| "a string".tap(&b) }.to yield_with_args(/str/)
expect { |b| [1, 2, 3].each(&b) }.to yield_successive_args(1, 2, 3)
expect { |b| { :a => 1, :b => 2 }.each(&b) }.to yield_successive_args([:a, 1], [:b, 2])
述語(真偽を返すメソッドを使って判定)
expect(actual).to be_xxx # passes if actual.xxx?
expect(actual).to have_xxx(:arg) # passes if actual.has_xxx?(:arg)
範囲 (Ruby >= 1.9 only)
expect(1..10).to cover(3)
コレクションの要素
# exact order, entire collection
expect(actual).to eq(expected)
# exact order, partial collection (based on an exact position)
expect(actual).to start_with(expected)
expect(actual).to end_with(expected)
# any order, entire collection
expect(actual).to match_array(expected)
# You can also express this by passing the expected elements
# as individual arguments
expect(actual).to contain_exactly(expected_element1, expected_element2)
# any order, partial collection
expect(actual).to include(expected)
例
expect([1, 2, 3]).to eq([1, 2, 3]) # Order dependent equality check
expect([1, 2, 3]).to include(1) # Exact ordering, partial collection matches
expect([1, 2, 3]).to include(2, 3) #
expect([1, 2, 3]).to start_with(1) # As above, but from the start of the collection
expect([1, 2, 3]).to start_with(1, 2) #
expect([1, 2, 3]).to end_with(3) # As above but from the end of the collection
expect([1, 2, 3]).to end_with(2, 3) #
expect({:a => 'b'}).to include(:a => 'b') # Matching within hashes
expect("this string").to include("is str") # Matching within strings
expect("this string").to start_with("this") #
expect("this string").to end_with("ring") #
expect([1, 2, 3]).to contain_exactly(2, 3, 1) # Order independent matches
expect([1, 2, 3]).to match_array([3, 2, 1]) #
# Order dependent compound matchers
expect(
[{:a => 'hash'},{:a => 'another'}]
).to match([a_hash_including(:a => 'hash'), a_hash_including(:a => 'another')])
and/or
expect(alphabet).to start_with("a").and end_with("z")
expect(stoplight.color).to eq("red").or eq("green").or eq("yellow")
a_xxx_yyy
マッチャyyyを満たすxxxを柔軟に組み立てることができる。
Examples
expect { k += 1.05 }.to change { k }.by( a_value_within(0.1).of(1.0) )
expect { s = "barn" }.to change { s }
.from( a_string_matching(/foo/) )
.to( a_string_matching(/bar/) )
expect(["barn", 2.45]).to contain_exactly(
a_value_within(0.1).of(2.5),
a_string_starting_with("bar")
)
expect(["barn", "food", 2.45]).to end_with(
a_string_matching("foo"),
a_value > 2
)
expect(["barn", 2.45]).to include( a_string_starting_with("bar") )
expect(:a => "food", :b => "good").to include(:a => a_string_matching(/foo/))
hash = {
:a => {
:b => ["foo", 5],
:c => { :d => 2.05 }
}
}
expect(hash).to match(
:a => {
:b => a_collection_containing_exactly(
a_string_starting_with("f"),
an_instance_of(Integer)
),
:c => { :d => (a_value < 3) }
}
)
expect { |probe|
[1, 2, 3].each(&probe)
}.to yield_successive_args( a_value < 2, 2, a_value > 2 )