- RSpec 教程
- RSpec - 首页
- RSpec - 简介
- RSpec - 基本语法
- RSpec - 编写规范
- RSpec - 匹配器
- RSpec - 测试替身
- RSpec - 存根
- RSpec - 钩子
- RSpec - 标签
- RSpec - 主题
- RSpec - 助手
- RSpec - 元数据
- RSpec - 过滤
- RSpec - 期望
- RSpec 资源
- RSpec - 快速指南
- RSpec - 有用资源
- RSpec - 讨论
RSpec - 匹配器
如果你还记得我们最初的 Hello World 示例,它包含一行如下所示的代码 -
expect(message).to eq "Hello World!"
关键字 eql 是一个 **RSpec** “匹配器”。在这里,我们将介绍 RSpec 中的其他类型的匹配器。
相等性/同一性匹配器
用于测试对象或值相等性的匹配器。
| 匹配器 | 描述 | 示例 |
|---|---|---|
| eq | 当 actual == expected 时通过 | 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
比较匹配器
用于比较两个值的匹配器。
| 匹配器 | 描述 | 示例 |
|---|---|---|
| > | 当 actual > expected 时通过 | expect(actual).to be > expected |
| >= | 当 actual >= expected 时通过 | expect(actual).to be >= expected |
| < | 当 actual < expected 时通过 | expect(actual).to be < expected |
| <= | 当 actual <= expected 时通过 | expect(actual).to be <= expected |
| be_between 包含 | 当 actual <= min 且 >= max 时通过 | expect(actual).to be_between(min, max).inclusive |
| be_between 排除 | 当 actual < min 且 > max 时通过 | expect(actual).to be_between(min, max).exclusive |
| match | 当 actual 匹配正则表达式时通过 | 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 | 当 actual 是预期类的实例时通过。 | expect(actual).to be_instance_of(Expected) |
| be_kind_of | 当 actual 是预期类的实例或其任何父类的实例时通过。 | expect(actual).to be_kind_of(Expected) |
| respond_to | 当 actual 对指定方法做出响应时通过。 | 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
真/假/空匹配器
用于测试值是否为真、假或空。
| 匹配器 | 描述 | 示例 |
|---|---|---|
| be true | 当 actual == true 时通过 | expect(actual).to be true |
| be false | 当 actual == false 时通过 | expect(actual).to be false |
| be_truthy | 当 actual 不为 false 或 nil 时通过 | expect(actual).to be_truthy |
| be_falsey | 当 actual 为 false 或 nil 时通过 | expect(actual).to be_falsey |
| be_nil | 当 actual 为 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
广告