RSpec - 匹配器
如果您还记得我们最初的 Hello World 示例,它包含如下一行 −
expect(message).to eq "Hello World!"
关键字 eql 是一个 RSpec"匹配器"。 在这里,我们将介绍 RSpec 中其他类型的匹配器。
平等/身份匹配器
用于测试对象或值是否相等的匹配器。
| 匹配器 | 描述 | 示例 |
|---|---|---|
| eq | 当实际 == 预期时通过 | expect(actual).to eq expected |
| eql | 当 actual.eql?(expected) 时通过 | expect(actual).to eql expected |
| be | 当 actual.equal?(expected) 时通过 | expect(actual).to be expected |
| equal | 当 actual.equal?(expected) 时也通过 | expect(actual).to equal expected |
示例
describe "An example of the equality Matchers" do
it "should show how the equality Matchers work" do
a = "test string"
b = a
# The following Expectations will all pass
expect(a).to eq "test string"
expect(a).to eql "test string"
expect(a).to be b
expect(a).to equal b
end
end
执行上述代码时,将产生以下输出。 您的计算机上的秒数可能略有不同 −
. Finished in 0.036 seconds (files took 0.11901 seconds to load) 1 example, 0 failures
比较匹配器
用于与值进行比较的匹配器。
| 匹配器 | 描述 | 示例 |
|---|---|---|
| > | 当实际 > 预期时通过 | expect(actual).to be > expected |
| >= | 当实际 >= 预期时通过 | expect(actual).to be >= expected |
| < | 当实际 < 预期时通过 | expect(actual).to be < expected |
| <= | 当实际 <= 预期时通过 | expect(actual).to be <= expected |
| be_between inclusive | 实际为 <= min and >= max 时通过 | expect(actual).to be_between(min, max).inclusive |
| be_between exclusive | 当实际为 < min and > max 时通过 | expect(actual).to be_between(min, max).exclusive |
| match | 当实际匹配正则表达式时通过 | expect(actual).to match(/regex/) |
示例
describe "An example of the comparison Matchers" do
it "should show how the comparison Matchers work" do
a = 1
b = 2
c = 3
d = 'test string'
# The following Expectations will all pass
expect(b).to be > a
expect(a).to be >= a
expect(a).to be < b
expect(b).to be <= b
expect(c).to be_between(1,3).inclusive
expect(b).to be_between(1,3).exclusive
expect(d).to match /TEST/i
end
end
执行上述代码时,将产生以下输出。 您的计算机上的秒数可能略有不同 −
. Finished in 0.013 seconds (files took 0.11801 seconds to load) 1 example, 0 failures
类/类型匹配器
用于测试对象类型或类的匹配器。
| 匹配器 | 描述 | 示例 |
|---|---|---|
| be_instance_of | 当实际是预期类的实例时通过。 | expect(actual).to be_instance_of(Expected) |
| be_kind_of | 当实际是预期类或其任何父类的实例时通过。 | expect(actual).to be_kind_of(Expected) |
| respond_to | 当实际响应指定方法时通过。 | expect(actual).to respond_to(expected) |
示例
describe "An example of the type/class Matchers" do
it "should show how the type/class Matchers work" do
x = 1
y = 3.14
z = 'test string'
# The following Expectations will all pass
expect(x).to be_instance_of Fixnum
expect(y).to be_kind_of Numeric
expect(z).to respond_to(:length)
end
end
执行上述代码时,将产生以下输出。 您的计算机上的秒数可能略有不同 −
. Finished in 0.002 seconds (files took 0.12201 seconds to load) 1 example, 0 failures
True/False/Nil 匹配器
用于测试值是否为 true、false 或 nil(零) 的匹配器。
| 匹配器 | 描述 | 示例 |
|---|---|---|
| be true | 当实际 == true 时通过 | expect(actual).to be true |
| be false | 当实际 == false 时通过 | expect(actual).to be false |
| be_truthy | 当实际不为 false 或 nil 时通过 | expect(actual).to be_truthy |
| be_falsey | 当实际为 false 或 nil 时通过 | expect(actual).to be_falsey |
| be_nil | 当实际值为 nil 时通过 | expect(actual).to be_nil |
示例
describe "An example of the true/false/nil Matchers" do
it "should show how the true/false/nil Matchers work" do
x = true
y = false
z = nil
a = "test string"
# The following Expectations will all pass
expect(x).to be true
expect(y).to be false
expect(a).to be_truthy
expect(z).to be_falsey
expect(z).to be_nil
end
end
执行上述代码时,将产生以下输出。 您的计算机上的秒数可能略有不同 −
. Finished in 0.003 seconds (files took 0.12301 seconds to load) 1 example, 0 failures
错误匹配器
当代码块引发错误时,用于测试的匹配器。
| 匹配器 | 描述 | 示例 |
|---|---|---|
| raise_error(ErrorClass) | 当块引发 ErrorClass 类型的错误时通过。 | expect {block}.to raise_error(ErrorClass) |
| raise_error("error message") | 当块引发错误并显示"error message"消息时通过。 | expect {block}.to raise_error(“error message”) |
| raise_error(ErrorClass, "error message") | 当块引发 ErrorClass 类型的错误且消息为"error message"时通过 | expect {block}.to raise_error(ErrorClass,“error message”) |
示例
将以下代码保存到名为 error_matcher_spec.rb 的文件中,并使用此命令运行它 − rspec error_matcher_spec.rb.
describe "An example of the error Matchers" do
it "should show how the error Matchers work" do
# The following Expectations will all pass
expect { 1/0 }.to raise_error(ZeroDivisionError)
expect { 1/0 }.to raise_error("divided by 0")
expect { 1/0 }.to raise_error("divided by 0", ZeroDivisionError)
end
end
执行上述代码时,将产生以下输出。 您的计算机上的秒数可能略有不同 −
. Finished in 0.002 seconds (files took 0.12101 seconds to load) 1 example, 0 failures

