- RSpec 教程
- RSpec - 首页
- RSpec - 简介
- RSpec - 基本语法
- RSpec - 编写规范
- RSpec - 匹配器
- RSpec - 测试替身
- RSpec - 存根
- RSpec - 钩子
- RSpec - 标签
- RSpec - 主题
- RSpec - 助手
- RSpec - 元数据
- RSpec - 过滤
- RSpec - 期望
- RSpec 资源
- RSpec - 快速指南
- RSpec - 有用资源
- RSpec - 讨论
RSpec - 过滤
您可能希望在阅读本节之前阅读有关 RSpec 元数据的部分,因为事实证明,RSpec 过滤基于 RSpec 元数据。
假设您有一个规范文件,其中包含两种类型的测试(示例):正向功能测试和负向(错误)测试。让我们这样定义它们:
RSpec.describe "An Example Group with positive and negative Examples" do
context 'when testing Ruby\'s build-in math library' do
it 'can do normal numeric operations' do
expect(1 + 1).to eq(2)
end
it 'generates an error when expected' do
expect{1/0}.to raise_error(ZeroDivisionError)
end
end
end
现在,将以上文本保存为名为“filter_spec.rb”的文件,然后使用以下命令运行它:
rspec filter_spec.rb
您将看到如下所示的输出:
.. Finished in 0.003 seconds (files took 0.11201 seconds to load) 2 examples, 0 failures
现在,如果我们只想重新运行此文件中的正向测试怎么办?或者只有负向测试?我们可以使用 RSpec 过滤器轻松做到这一点。将以上代码更改为:
RSpec.describe "An Example Group with positive and negative Examples" do
context 'when testing Ruby\'s build-in math library' do
it 'can do normal numeric operations', positive: true do
expect(1 + 1).to eq(2)
end
it 'generates an error when expected', negative: true do
expect{1/0}.to raise_error(ZeroDivisionError)
end
end
end
保存您对 filter_spec.rb 的更改并运行此略有不同的命令:
rspec --tag positive filter_spec.rb
现在,您将看到如下所示的输出:
Run options: include {:positive=>true}
.
Finished in 0.001 seconds (files took 0.11401 seconds to load)
1 example, 0 failures
通过指定 --tag positive,我们告诉 RSpec 只运行定义了 :positive 元数据变量的示例。我们可以通过运行以下命令对负向测试执行相同的操作:
rspec --tag negative filter_spec.rb
请记住,这些仅仅是示例,您可以使用任何您想要的名称指定过滤器。
RSpec 格式化程序
格式化程序允许 RSpec 以不同的方式显示测试的输出。让我们创建一个包含以下代码的新 RSpec 文件:
RSpec.describe "A spec file to demonstrate how RSpec Formatters work" do
context 'when running some tests' do
it 'the test usually calls the expect() method at least once' do
expect(1 + 1).to eq(2)
end
end
end
现在,将其保存到名为 formatter_spec.rb 的文件中并运行此 RSpec 命令:
rspec formatter_spec.rb
您应该看到如下所示的输出:
. Finished in 0.002 seconds (files took 0.11401 seconds to load) 1 example, 0 failures
现在运行相同的命令,但这次指定一个格式化程序,如下所示:
rspec --format progress formatter_spec.rb
您应该这次看到相同的输出:
. Finished in 0.002 seconds (files took 0.11401 seconds to load) 1 example, 0 failures
原因是“progress”格式化程序是默认格式化程序。接下来让我们尝试一个不同的格式化程序,尝试运行此命令:
rspec --format doc formatter_spec.rb
现在您应该看到此输出:
A spec file to demonstrate how RSpec Formatters work
when running some tests
the test usually calls the expect() method at least once
Finished in 0.002 seconds (files took 0.11401 seconds to load)
1 example, 0 failures
如您所见,使用“doc”格式化程序的输出完全不同。此格式化程序以类似文档的样式呈现输出。您可能想知道当测试(示例)失败时这些选项是什么样的。让我们更改formatter_spec.rb中的代码使其如下所示:
RSpec.describe "A spec file to demonstrate how RSpec Formatters work" do
context 'when running some tests' do
it 'the test usually calls the expect() method at least once' do
expect(1 + 1).to eq(1)
end
end
end
期望expect(1 + 1).to eq(1)应该失败。保存您的更改并重新运行以上命令:
rspec --format progress formatter_spec.rb并记住,由于“progress”格式化程序是默认的,因此您可以只运行:rspec formatter_spec.rb。您应该看到此输出:
F
Failures:
1) A spec file to demonstrate how RSpec Formatters work when running some tests
the test usually calls the expect() method at least once
Failure/Error: expect(1 + 1).to eq(1)
expected: 1
got: 2
(compared using ==)
# ./formatter_spec.rb:4:in `block (3 levels) in <top (required)>'
Finished in 0.016 seconds (files took 0.11201 seconds to load)
1 example, 1 failure
Failed examples:
rspec ./formatter_spec.rb:3 # A spec file to demonstrate how RSpec
Formatters work when running some tests the test usually calls
the expect() method at least once
现在,让我们尝试 doc 格式化程序,运行此命令:
rspec --format doc formatter_spec.rb
现在,在测试失败的情况下,您应该看到此输出:
A spec file to demonstrate how RSpec Formatters work
when running some tests
the test usually calls the expect() method at least once (FAILED - 1)
Failures:
1) A spec file to demonstrate how RSpec Formatters work when running some
tests the test usually calls the expect() method at least once
Failure/Error: expect(1 + 1).to eq(1)
expected: 1
got: 2
(compared using ==)
# ./formatter_spec.rb:4:in `block (3 levels) in <top (required)>'
Finished in 0.015 seconds (files took 0.11401 seconds to load)
1 example, 1 failure
失败的示例
rspec ./formatter_spec.rb:3 # 一个规范文件,用于演示 RSpec 格式化程序在运行一些测试时如何工作,测试通常至少调用一次 expect() 方法。
RSpec 格式化程序提供了更改测试结果显示方式的功能,甚至可以创建自己的自定义格式化程序,但这是一个更高级的主题。