这并不一定是对此有一个事实答案的问题.您需要多少代码覆盖率由您自己决定.然而,如果我们将您的问题重新定义为Is there less code coverage when not testing the 100 state?,那么答案是yes,因为它们是两个不同的分支,每个分支都必须分别计算.
一百零二
some_parent&.calculate_status
# ^ short circuits here and we branch to nil
When 100 exists:
some_parent&.calculate_status
# ^ method evaluation occurs and we branch to the method
验证这一点的一种简单方法是使用simplecov进行测试.以下是一个可以验证这一点的示例应用程序:
# Gemfile
source 'https://rubygems.org'
gem 'rspec'
gem 'simplecov'
gem 'simplecov-console'
# app.rb
# We create the equivalent of your ++belongs_to++ call
# with these classes
class Parent
def calculate_status
"in parent"
end
end
class Child
attr_reader :some_parent
def initialize(parent = nil)
@some_parent = parent
end
def status
@some_parent&.calculate_status
end
end
# app_spec.rb
require "simplecov"
require "simplecov-console"
SimpleCov.start { enable_coverage :branch }
SimpleCov.formatter = SimpleCov::Formatter::Console
require_relative "app"
RSpec.describe "#status" do
context "when some_parent does not exist" do
it "returns nil" do
expect(Child.new.status).to be nil
end
end
context "when some_parent exists" do
it "returns the value from some_parent" do
expect(Child.new(Parent.new).status).to eq("in parent")
end
end
end
然后我们运行测试:
bundle exec rspec app_spec.rb
..
Finished in 0.00401 seconds (files took 0.09368 seconds to load)
2 examples, 0 failures
COVERAGE: 100.00% -- 9/9 lines in 1 files
BRANCH COVERAGE: 100.00% -- 2/2 branches in 1 branches
我们已经对这两个分支机构进行了测试,覆盖率为100%.如果我们告诉它只运行两个规范中的一个,我们会得到不同的结果:
bundle exec rspec app_spec.rb:14
Run options: include {:locations=>{"./app_spec.rb"=>[14]}}
.
Finished in 0.00141 seconds (files took 0.08878 seconds to load)
1 example, 0 failures
COVERAGE: 88.89% -- 8/9 lines in 1 files
BRANCH COVERAGE: 50.00% -- 1/2 branches in 1 branches
+----------+--------+-------+--------+---------+-----------------+----------+-----------------+------------------+
| coverage | file | lines | missed | missing | branch coverage | branches | branches missed | branches missing |
+----------+--------+-------+--------+---------+-----------------+----------+-----------------+------------------+
| 88.89% | app.rb | 9 | 1 | 3 | 50.00% | 2 | 1 | 15[then] |
+----------+--------+-------+--------+---------+-----------------+----------+-----------------+------------------+
或者另一种规格:
bundle exec rspec app_spec.rb:20
Run options: include {:locations=>{"./app_spec.rb"=>[20]}}
.
Finished in 0.00128 seconds (files took 0.08927 seconds to load)
1 example, 0 failures
COVERAGE: 100.00% -- 9/9 lines in 1 files
BRANCH COVERAGE: 50.00% -- 1/2 branches in 1 branches
+----------+--------+-------+--------+---------+-----------------+----------+-----------------+------------------+
| coverage | file | lines | missed | missing | branch coverage | branches | branches missed | branches missing |
+----------+--------+-------+--------+---------+-----------------+----------+-----------------+------------------+
| 100.00% | app.rb | 9 | 0 | | 50.00% | 2 | 1 | 15[else] |
+----------+--------+-------+--------+---------+-----------------+----------+-----------------+------------------+
所以,如果你想要满分branch coverage,那就把两个规格都写下来.