内置匹配器

rspec-expectations 附带了许多内置匹配器。每个匹配器都可以与 expect(..).toexpect(..).not_to 一起使用,分别在对象上定义正向和负向期望。大多数匹配器也可以使用 (...).should(...).should_not 语法访问;有关我们建议使用 expect 的原因,请参阅 使用 should 语法

例如

expect(result).to   eq(3)
expect(list).not_to be_empty
pi.should be > 3

对象标识

expect(actual).to be(expected) # passes if actual.equal?(expected)

对象等价性

expect(actual).to eq(expected) # passes if actual == expected

标识/等价性的可选 API

expect(actual).to eql(expected)   # passes if actual.eql?(expected)
expect(actual).to equal(expected) # passes if actual.equal?(expected)

# NOTE: `expect` does not support `==` matcher.

比较

expect(actual).to be >  expected
expect(actual).to be >= expected
expect(actual).to be <= expected
expect(actual).to be <  expected
expect(actual).to be_between(minimum, maximum).inclusive
expect(actual).to be_between(minimum, maximum).exclusive
expect(actual).to match(/expression/)
expect(actual).to be_within(delta).of(expected)
expect(actual).to start_with expected
expect(actual).to end_with expected

# NOTE: `expect` does not support `=~` matcher.

类型/类/响应

expect(actual).to be_instance_of(expected)
expect(actual).to be_kind_of(expected)
expect(actual).to respond_to(expected)

真值和存在主义

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_falsey    # 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 exist        # passes if actual.exist? and/or actual.exists? are truthy
expect(actual).to exist(*args) # passes if actual.exist?(*args) and/or actual.exists?(*args) are truthy

期望错误

expect { ... }.to raise_error
expect { ... }.to raise_error(ErrorClass)
expect { ... }.to raise_error("message")
expect { ... }.to raise_error(ErrorClass, "message")

期望抛出

expect { ... }.to throw_symbol
expect { ... }.to throw_symbol(:symbol)
expect { ... }.to throw_symbol(:symbol, 'value')

谓词匹配器

expect(actual).to be_xxx         # passes if actual.xxx?
expect(actual).to have_xxx(:arg) # passes if actual.has_xxx?(:arg)

示例

expect([]).to      be_empty
expect(:a => 1).to have_key(:a)

集合成员资格

expect(actual).to include(expected)
expect(array).to match_array(expected_array)
# ...which is the same as:
expect(array).to contain_exactly(individual, elements)

示例

expect([1, 2, 3]).to     include(1)
expect([1, 2, 3]).to     include(1, 2)
expect(:a => 'b').to     include(:a => 'b')
expect("this string").to include("is str")
expect([1, 2, 3]).to     contain_exactly(2, 1, 3)
expect([1, 2, 3]).to     match_array([3, 2, 1])

范围 (仅限 1.9+)

expect(1..10).to cover(3)

变更观察

expect { object.action }.to change(object, :value).from(old).to(new)
expect { object.action }.to change(object, :value).by(delta)
expect { object.action }.to change(object, :value).by_at_least(minimum_delta)
expect { object.action }.to change(object, :value).by_at_most(maximum_delta)

示例

expect { a += 1 }.to change { a }.by(1)
expect { a += 3 }.to change { a }.from(2)
expect { a += 3 }.to change { a }.by_at_least(2)

满足

expect(actual).to satisfy { |value| value == expected }

输出捕获

expect { actual }.to output("some output").to_stdout
expect { actual }.to output("some error").to_stderr

块期望

expect { |b| object.action(&b) }.to yield_control
expect { |b| object.action(&b) }.to yield_with_no_args           # only matches no args
expect { |b| object.action(&b) }.to yield_with_args              # matches any args
expect { |b| object.action(&b) }.to yield_successive_args(*args) # matches args against multiple yields

示例

expect { |b| User.transaction(&b) }.to yield_control
expect { |b| User.transaction(&b) }.to yield_with_no_args
expect { |b| 5.tap(&b)            }.not_to yield_with_no_args         # because it yields with `5`
expect { |b| 5.tap(&b)            }.to yield_with_args(5)             # because 5 == 5
expect { |b| 5.tap(&b)            }.to yield_with_args(Integer)       # because Integer === 5
expect { |b| [1, 2, 3].each(&b)   }.to yield_successive_args(1, 2, 3)

主题