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

标签 ruby-on-rails rspec simplecov

我决定尝试使用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/

相关文章:

ruby-on-rails - 如何让 capybara 找到快速出现和消失的元素(加载微调器)

ruby-on-rails - 访问请求规范中的 session 哈希

simplecov - 如何配置SimpleCov忽略#inspect方法

ruby-on-rails-4 - Simplecov 报告忽略/app 目录中的文件夹

ruby-on-rails - 模型文件中的 Ruby on Rails IBAN 验证

ruby-on-rails - 如何在 rails_admin 的字段上启用 CKEditor?

ruby-on-rails - 为 Rails App 中的某些用户设计强制注销

javascript - Rails 浏览器语法高亮插件?

ruby-on-rails - 如何组合 simplecov coverage/index.html 文件

ruby-on-rails - 使用 ActiveRecord::Relation 时的 RSpec 匹配器