gpt4 book ai didi

ruby-on-rails - SimpleCov计算用户模型的0%覆盖率

转载 作者:行者123 更新时间:2023-12-03 23:31:55 25 4
gpt4 key购买 nike

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

我有用户模型,有包含测试用例的user_spec.rb,但是simplecov显示此模型的覆盖率是0%。而且它显示了其他型号的100%覆盖率,这是事实。我不了解用户模型有什么问题。

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

关于ruby-on-rails - SimpleCov计算用户模型的0%覆盖率,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35476814/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com