RSpec – 匹配器
RSpec – 匹配器
如果您还记得我们最初的 Hello World 示例,它包含一行如下所示 –
expect(message).to eq "Hello World!"
关键字 eql 是一个RSpec “匹配器”。在这里,我们将介绍 RSpec 中其他类型的匹配器。
平等/身份匹配器
匹配器来测试对象或值是否相等。
| Matcher | 描述 | 例子 |
|---|---|---|
| eq | 当实际 == 预期时通过 | 期望(实际)。等于预期 |
| eql | 当 actual.eql?(expected) 时通过 | 期望(实际).to eql 预期 |
| be | 当实际.相等时通过?(预期) | 预期(实际)。预期 |
| equal | 当 actual.equal 时也通过?(预期) | 期望(实际)。等于预期 |
例子
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
比较匹配器
用于比较值的匹配器。
| Matcher | 描述 | 例子 |
|---|---|---|
| > | 当实际 > 预期时通过 | 期望(实际)。要> 预期 |
| >= | 当实际 >= 预期时通过 | 期望(实际)。要> = 预期 |
| < | 当实际<预期时通过 | 期望(实际)。< 预期 |
| <= | 当实际 <= 预期时通过 | 期望(实际)。要<= 预期 |
| be_between inclusive | 当实际值 <= min 且 >= max 时通过 | 期望(实际).to be_between(min, max).inclusive |
| be_between exclusive | 当实际值为 < min 和 > max 时通过 | 期望(实际).to be_between(min, max).exclusive |
| 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
类/类型匹配器
用于测试对象类型或类别的匹配器。
| Matcher | 描述 | 例子 |
|---|---|---|
| be_instance_of | 当实际是预期类的实例时通过。 | 期望(实际).to be_instance_of(预期) |
| be_kind_of | 当实际是预期类或其任何父类的实例时通过。 | 期望(实际)。成为_kind_of(预期) |
| respond_to | 当实际响应指定的方法时通过。 | 期望(实际)。响应(预期) |
例子
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 的匹配器。
| Matcher | 描述 | 例子 |
|---|---|---|
| be true | 当实际 == 真时通过 | 期望(实际)是真的 |
| be false | 当实际 == 假时通过 | 期望(实际)是假的 |
| be_truthy | 当 actual 不是 false 或 nil 时通过 | 期望(实际)。成为_真实 |
| be_falsey | 当 actual 为 false 或 nil 时通过 | 期望(实际).to be_falsey |
| be_nil | 当实际为零时通过 | 期望(实际).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
错误匹配器
用于测试的匹配器,当代码块引发错误时。
| Matcher | 描述 | 例子 |
|---|---|---|
| raise_error(ErrorClass) | 当块引发 ErrorClass 类型的错误时通过。 | 期望 {block}.to raise_error(ErrorClass) |
| raise_error(“error message”) | 当块引发带有“错误消息”消息的错误时通过。 | 期望 {block}.to raise_error(“错误信息”) |
| raise_error(ErrorClass, “error message”) | 当块引发带有消息“错误消息”的 ErrorClass 类型的错误时通过 | 期望 {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