[英]SimpleCov calculate 0% coverage for user model
我决定尝试使用simplecov gem。 我认为这是一个很酷的工具,但我有一个问题:
我有模型 User 和包含测试用例的 user_spec.rb,但 simplecov 显示该模型的覆盖率为 0%。 它显示了其他模型的 100% 覆盖率,这是真的。 我不明白 User 模型有什么问题。
class User < ActiveRecord::Base
extend Enumerize
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
STATUS_ACTIVE = :active
STATUS_BANNED = :banned
enumerize :status, in: [STATUS_ACTIVE, STATUS_BANNED], default: STATUS_ACTIVE
with_options inverse_of: :user, dependent: :destroy do
has_one :profile
has_many :articles
end
before_create :build_default_profile
private
def build_default_profile
build_profile
end
end
用户规范.rb
require 'rails_helper'
RSpec.describe User, type: :model do
describe '#validations' do
it { should have_one(:profile).dependent(:destroy) }
it { should validate_presence_of(:email) }
it { should validate_presence_of(:password) }
it { should validate_confirmation_of(:password) }
it { should enumerize(:status).in(User::STATUS_ACTIVE, User::STATUS_BANNED).with_default(User::STATUS_ACTIVE) }
#TODO other devise validations
end
describe '#callbacks' do
it 'creates profile after_create' do
user = build(:user)
expect(user.profile).to be_nil
user.save
expect(user.profile).to be_a(Profile)
end
it 'must not create profile after update' do
user = create(:user)
profile = user.profile
user.email = Faker::Internet.email
user.save
expect(profile.id).to eq(Profile.find_by(user_id: user.id).id)
end
end
end
覆盖面
File % covered Lines Relevant Lines Lines covered Lines missed Avg. Hits / Line
app/models/user.rb 0.0 % 28 28 0 28 0.0
app/models/admin.rb 100.0 % 3 1 1 0 1.0
app/models/article.rb 100.0 % 32 19 19 0 5.8
app/models/profile.rb 100.0 % 13 6 6 0 1.0
确保您正确启动 SimpleCov。 在你的情况下,
在的最顶部加载并启动 SimpleCov
查看更多: https : //github.com/colszowka/simplecov#getting-started
它仅在我使用 spring 时发生,实际上是当我使用spring-commands-rspec
gem 生成的 rspec binstub 时。 尝试使用命令spring stop
停止 spring 并使用rspec spec
再次运行rspec spec
。
我有一个类似的问题。 我有当前的 simplecov 0.17.1。
我在默认设置下使用 Rails 6(Minitest 和 Spring,没有 rspec),我用rails test
运行我的rails test
。
我已经尝试了所有其他答案,但没有成功。
simplecov 可能有问题: https : //github.com/colszowka/simplecov/issues/671
我正在尝试像fastcov这样的替代方案
编辑1
fastcov 似乎是 simplecov 的轻量复制品,一点都不成熟。 它还没有发布! 他们是 simplecov 的替代品吗?!
编辑2
我设法通过添加到bin/rails
的顶部使其工作
#!/usr/bin/env ruby
if ENV['RAILS_ENV'] == 'test'
require 'simplecov'
SimpleCov.start 'rails'
puts "required simplecov"
end
# ...
并且在test_helper.rb
,我设置了parallelize(workers: 1)
# test/test_helper.rb
require 'simplecov'
SimpleCov.start 'rails'
ENV['RAILS_ENV'] ||= 'test'
require_relative '../config/environment'
require 'rails/test_help'
class ActiveSupport::TestCase
parallelize(workers: 1)
fixtures :all
end
我使用命令RAILS_ENV=test rails test
你必须像这样创建一个初始化程序:
配置/初始化程序/simplecov.rb
if ENV['RAILS_ENV'] == 'test'
require 'simplecov'
SimpleCov.start 'rails'
puts "required simplecov"
end
我遇到了同样的问题,刚刚在这里找到答案: https : //github.com/colszowka/simplecov/issues/82
要求应该在加载其他任何东西之前发生。 就我而言,我有:
require simplecov SimpleCov.start 'rails'
后:
require File.expand_path('../../config/environment', __FILE__)
这可能使设计模块没有被加载。 一旦我将“require simplecov”和“simplecov.start”移到 rails_helper 的开头,它就会按预期工作。
simplecov 显示的指标是在运行测试用例的过程中调用的行数。 例如,如果我有:
class Test
def method
'Response'
end
end
RSpec.describe Test, type: :model do
context '#method' do
let(:test) { Test.new }
it 'returns response' do
expect(test.method).to eq('Response')
end
end
end
simplecov 将显示 100% 的覆盖率,因为当我运行我的规范时,它会命中 Test 类中的每一行。 对于您的用户类,您的规范实际上并没有调用用户类中的任何行,因为您没有任何相关的行(它不认为您的私有方法是相关的)。
我不会担心您的用户模型的 0% 覆盖率,因为您进行的测试似乎非常全面。
我看到了同样的问题,我认为它与 Spring rspec
binstubs 有关。 我正在使用spring-commands-rspec
gem 并在bin/spring
有一个用于 rspec 的 binstub 。 创建该 binstub 后,我的 Simplecov 测试覆盖率计算下降了 10%,并显示我的User
模型的覆盖率为 0%。 当我删除(或重命名也有效) bin/spring
脚本并重新运行rspec
,我的覆盖范围恢复了。
您是否使用spring-commands-rspec
或任何其他 Spring binstub 来运行您的测试? 一旦我弄清楚是否有解决方法,我会发布更多。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.