在阅读本节之前,您可能需要阅读有关RSpec元数据(Metadata)的部分,因为事实证明,RSpec筛选(Filtering)基于RSpec元数据。
假设您有一个spec文件,它包含两种类型的测试:正确测试和错误测试。让无涯教程这样定义它们-
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肯定,无涯教程告诉RSpec仅运行positive示例。可以通过运行以下命令对negative示例相同的操作-
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
接下来尝试其他格式化程序,尝试运行此命令-
rspec --format doc formatter_spec.rb
现在您应该看到此输出-
A spec file to demonstrate how RSpec格式化程序 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 Formatter 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
对eq(1)的期望 expect(1 +1),会失败。保存更改并重新运行以上命令-
rspec --format progress formatter_spec.rb ,请记住,由于" progress"格式化程序是默认格式,因此您可以运行: rspec formatter_spec.rb 。您应该看到此输出-
F Failures: 1) A spec file to demonstrate how RSpec Formatter 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 Formatter 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 Formatter 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
参考链接
https://www.learnfk.com/rspec/rspec-filtering.html
标签:do,seconds,无涯,RSpec,expect,rb,过滤器,spec From: https://blog.51cto.com/u_14033984/8473221