【问题标题】:SimpleCov calculate 0% coverage for user modelSimpleCov 计算用户模型的 0% 覆盖率
【发布时间】:2016-02-18 08:56:52
【问题描述】:

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

我有一个模型 User,我有一个包含测试用例的 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

【问题讨论】:

  • 您使用什么命令来运行规范?您确定 user_spec.rb 也正在运行 - 如果您将 binding.pryputs "it works" 放入 '#callbacks' 测试中 - 它会停止/打印消息吗?
  • 是的,我确定,我在终端中使用了命令 rspec
  • 我也看到了这个问题。我正在使用 Devise,看起来你也是。我想知道它是否会以某种方式干扰。我正在使用 Devise 3.5.2,你呢? Simplecov 的版本是 0.11.1。这个问题刚刚开始发生在我身上。我的用户模型有大量测试。
  • @stephen.hanson 我已经为除了 0% 的用户之外的所有问题打了 100&。我可以制定的唯一假设是devise 以及 User 类既是访问对象又是访问驱动程序的事实。你解决了吗?
  • @Jerome 这是很久以前的事了,但如果我没记错的话,我认为问题出在 Spring 上。如果我运行 bundle exec rspec 而不是使用 Spring binstub,我认为覆盖率报告正确。

标签: ruby-on-rails rspec simplecov


【解决方案1】:

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

rails_helper.rb

的最顶部

加载并启动 SimpleCov

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

    • 当然,任何有同样问题的人都应该尝试这个和接受的答案。
    • 我必须使用 DISABLE_SPRING 环境变量禁用 spring 才能在运行覆盖时使其工作。
    【解决方案3】:

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

    我正在使用带有默认设置的 Rails 6(Minitest 和 Spring,没有 rspec),我使用 rails test 运行我的测试。

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

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

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

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

    edit2
    我设法通过添加到bin/rails 的顶部来使其工作

    #!/usr/bin/env ruby
    if ENV['RAILS_ENV'] == 'test'
      require 'simplecov'
      SimpleCov.start 'rails'
      puts "required simplecov"
    end
    # ...
    

    ANDtest_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 运行测试

    【讨论】:

    • 谢谢你!我对 Rails 6、Minitest 和 Spring 进行了相同的设置,并面临同样的问题。始终覆盖 0%。真正为我解决的是你关于将并行工作人员设置为 1 的提示。因为它的运行比预期的要好!
    • 简单地禁用 parallelize 似乎是解决方法,直到 simplecov 支持它 github.com/colszowka/simplecov/issues/718
    【解决方案4】:

    你必须像这样创建一个初始化器:

    config/initializers/simplecov.rb

    if ENV['RAILS_ENV'] == 'test'
      require 'simplecov'
      SimpleCov.start 'rails'
      puts "required simplecov"
    end
    

    【讨论】:

      【解决方案5】:

      我遇到了同样的问题,刚刚在这里找到了答案: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 的最开头,它就按预期工作了。

      【讨论】:

        【解决方案6】:

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

        【讨论】:

        • 感谢描述,但是很奇怪,模型Admin是模型User的后代,user_spec.rb和admin_spec.rb代码几乎一样,但是simplecov显示100%覆盖admin.rb和0% 的用户.rb
        • 嗯,这有点奇怪。您的管理模型中是否还有其他方法?
        • 不,管理员
        • 确实很奇怪。正如您在 simplecov 结果中看到的那样,它正在考虑 admin.rb 有 1 条相关行,这让我认为它正在考虑 build_default_profile 仅在管理类中相关。尝试使用 :novoc: 阻止它(在方法定义之前和之后),看看是否会改变结果。
        【解决方案7】:

        我看到了同样的问题,我认为这与 Spring rspec binstubs 有关。我正在使用spring-commands-rspec gem 并在bin/spring 中有一个用于 rspec 的 binstub。创建该 binstub 后,我的 Simplecov 测试覆盖率计算下降了 10%,并显示我的 User 模型的覆盖率为 0%。当我删除(或重命名工作)bin/spring 脚本并重新运行 rspec 时,我的覆盖范围恢复了。

        您是否使用 spring-commands-rspec 或任何其他 Spring binstubs 来运行您的测试?一旦我确定是否有解决方法,我会发布更多。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2013-02-19
          • 2020-08-12
          • 1970-01-01
          • 1970-01-01
          • 2019-01-19
          • 2015-10-06
          • 2020-02-12
          • 2017-02-06
          相关资源
          最近更新 更多