本文介绍了SimpleCov 计算用户模型的 0% 覆盖率的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我决定尝试使用 simplecov gem.我认为这是一个很酷的工具,但我有一个问题:

I decided to try using simplecov gem. And I think it's cool tool, but I have one problem:

我有模型 User 和包含测试用例的 user_spec.rb,但 simplecov 显示该模型的覆盖率为 0%.它显示了其他模型的 100% 覆盖率,这是真的.我不明白 User 模型有什么问题.

I have model User and I have user_spec.rb which contains test cases, but simplecov shows 0% coverage of this model. And It shows 100% coverage for other models, and It's true. I don't understand what's trouble with User model.

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

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.就你而言,

Make sure that you are starting SimpleCov correctly. In your case,

rails_helper.rb

查看更多:https://github.com/colszowka/simplecov#getting-started

这篇关于SimpleCov 计算用户模型的 0% 覆盖率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!