- RSpec - Expectations
- RSpec - Filtering
- RSpec - Metadata
- RSpec - Helpers
- RSpec - Subjects
- RSpec - Tags
- RSpec - Hooks
- RSpec - Stubs
- RSpec - Test Doubles
- RSpec - Matchers
- RSpec - Writing Specs
- RSpec - Basic Syntax
- RSpec - Introduction
- RSpec - Home
RSpec Resources
Selected Reading
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
选读
RSpec - Filtering
在阅读本节之前,你不妨读一下关于RSpec元数据的部分,因为从中可以看出,RSpec过滤是以RSpec元数据为基础的。
试想一下,你有光谱文档,其中含有两种类型的测试(实例):积极的功能测试和负面(error)测试。 让我们这样界定:
RSpec.describe "An Example Group with positive and negative Examples" do context when testing Ruby s build-in math pbrary 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
现在,将上述案文作为称为“过滤器_spec.rb”的档案予以保存,然后由该指挥部管理。
rspec filter_spec.rb
你们会看到这样的产出:
.. Finished in 0.003 seconds (files took 0.11201 seconds to load) 2 examples, 0 failures
现在,我们只想在这一档案中重新开始积极的试验? 或者只有消极检验? 我们很容易与RSpec Filters一道这样做。 将上述法典改为:
RSpec.describe "An Example Group with positive and negative Examples" do context when testing Ruby s build-in math pbrary 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
撤销对过滤器的改动,并管理这种稍有不同指挥——
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
通过具体说明——正数,我们仅把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
如今,如果把这保存在一份名为“格式”的档案中,并管理这个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 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
如你所知,产出与“思想”格式有很大不同。 这种格式以类似文件的方式列报产出。 如果在试验中失败(例例),你会想到这些选择是什么样子。 让我们改变在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)。 取消你们的改变,重新管理上述指挥——
spec -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
现在,让我们尝试手法形式,管理这一指挥——
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
Failed Examples
rspec /formatter_spec.rb:3 # 用于证明在进行某些测试时,RSpec格式如何发挥作用的光谱文件通常至少需要一次使用(预期)方法。
用户数据表提供了改变测试结果展示方式的能力,甚至有可能形成自己的习惯格式,但这是一个更先进的专题。
Advertisements