RSpec -Filtering

在阅读本节之前,您可能需要阅读有关RSpec元数据的部分,因为事实证明,RSpec过滤基于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

现在,如果,我们w只重新运行此文件中的正面测试?还是只有负面测试?我们可以使用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只运行带有:肯定元数据变量的例子.我们可以通过运行像这样的命令来做负面测试同样的事情;<

rspec --tag negative filter_spec.rb

请记住,这些只是示例,您可以指定任何名称所需的过滤器.

RSpec Formatters

格式化程序允许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

原因是"进展" "formatter是默认格式化程序.让我们接下来尝试不同的格式化程序,尝试运行此命令 :

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

预期期望(1 + 1).到eq(1)应该失败.保存更改并重新运行以上命令 :

rspec  - 格式化进度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

现在,失败的测试t,你应该看到这个输出 :

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 Formatters在运行某些测试时如何工作的spec文件,测试通常至少调用一次expect()方法.

RSpec Formatters提供能够改变测试结果的显示方式,甚至可以创建自己的自定义Formatter,但这是一个更高级的主题.