18

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

我有一个模型Useruser_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
4

7 回答 7

11

确保您正确启动 SimpleCov。在你的情况下,

rails_helper.rb的最顶部加载并启动 SimpleCov

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

于 2016-03-17T15:50:02.477 回答
9

只有当我使用 spring 时才会发生这种情况,实际上是当我使用 gem 生成的 rspec binstub 时spring-commands-rspec。尝试使用命令停止 springspring stop并再次使用rspec spec.

于 2016-03-12T07:26:16.043 回答
7

我有一个类似的问题。我有当前的 simplecov 0.17.1。

我正在使用具有默认设置(Minitest 和 Spring,没有 rspec)的 Rails 6,我使用rails test.

我已经尝试了所有其他答案但没有成功。

simplecov 可能有问题:https ://github.com/colszowka/simplecov/issues/671

我正在尝试像fastcov这样的替代方案

edit1
fastcov 似乎是 simplecov 的简单副本,一点也不成熟。它还没有发布!他们是simplecov的替代品吗?!

edit2
我设法通过添加到顶部来使其工作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

于 2019-11-04T22:42:25.337 回答
4

您必须像这样创建一个初始化程序:

配置/初始化程序/simplecov.rb

if ENV['RAILS_ENV'] == 'test'
  require 'simplecov'
  SimpleCov.start 'rails'
  puts "required simplecov"
end
于 2018-01-25T16:07:52.083 回答
2

我遇到了同样的问题,刚刚在这里找到了答案:https ://github.com/colszowka/simplecov/issues/82

require 应该在加载其他任何内容之前发生。就我而言,我有:

require simplecov SimpleCov.start 'rails'

后:

require File.expand_path('../../config/environment', __FILE__)

这可能使设计模块没有被加载。一旦我将“require simplecov”和“simplecov.start”移到 rails_helper 的开头,它就按预期工作了。

于 2016-05-01T12:50:24.240 回答
1

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% 覆盖率,因为您的测试看起来非常全面。

于 2016-02-18T09:41:28.860 回答
1

I was seeing the same issue, and I think it has something to do with Spring rspec binstubs. I'm using the spring-commands-rspec gem and have a binstub for rspec in bin/spring. After creating that binstub, my Simplecov test coverage calculations went down by 10% and showed that my User model had 0% coverage. When I deleted (or renaming works too) the bin/spring script and re-ran rspec, my coverage was back up.

Are you using spring-commands-rspec or any other Spring binstubs to run your tests? I'll post more once I figure out if there's a workaround.

于 2016-03-01T19:33:52.663 回答