SimpleCov计算用户模型的0%覆盖率
我决定尝试使用simplecov gem。 我认为这是很酷的工具,但我有一个问题:
我有模型用户,我有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
user_spec.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。 在你的情况下,
在 rails_helper.rb 的最顶部加载并启动SimpleCov
查看更多: https : //github.com/colszowka/simplecov#getting-started
只有当我使用spring时才会发生这种情况,实际上当我使用spring-commands-rspec
gem生成的rspec binstub时。 尝试使用命令spring stop
停止弹簧并使用rspec spec
规格再次运行规格。
你必须创建一个这样的启动器:
配置/初始化/ simplecov.rb
if ENV['RAILS_ENV'] == 'test' require 'simplecov' SimpleCov.start 'rails' puts "required simplecov" end
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来运行测试吗? 一旦我弄清楚是否有解决方法,我会发布更多内容。
我有同样的问题,只是在这里找到答案: 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的最开头,它就按预期工作了。