SimpleCov calculate 0% coverage for user model

偶尔善良 提交于 2019-12-04 23:56:35

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

Load and launch SimpleCov at the very top of your rails_helper.rb

See more: https://github.com/colszowka/simplecov#getting-started

It happens with me only when I use spring, actually when I use rspec binstub generated by spring-commands-rspec gem. Try to stop spring with command spring stop and run specs again with rspec spec.

You have to create an initilizer like this:

config/initializers/simplecov.rb

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

The metric that simplecov displays is the number of lines that get called in the process of running test cases. For example if I had:

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 will show 100% coverage because it is hitting every single line in the Test class when I run my specs. In the case of your user class, your specs don't actually invoke any lines in the user class because you don't have any relevant lines (it isn't considering your private method to be relevant).

I wouldn't worry about the 0% coverage for your user model as the tests you have seem pretty comprehensive.

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.

I had the same problem and just found the answer here: https://github.com/colszowka/simplecov/issues/82

The require should be happening before loading anything else. In my case I had:

require simplecov SimpleCov.start 'rails'

after:

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

which probably made the devise modules not being loaded. As soon as I moved the "require simplecov" and "simplecov.start" to the very beginning of rails_helper, it worked as expected.

I have a similar issue. I have the current simplecov 0.17.1.

I'm using Rails 6 with the default setup (Minitest and Spring, no rspec), I run my tests with rails test.

I have try all the other answers without success.

simplecov may be buggy: https://github.com/colszowka/simplecov/issues/671

I'm trying alternative like fastcov

edit1
fastcov seems to be a ligthen copy of simplecov, not mature at all. It's not released yet! Is their any alternative to simplecov?!

edit2
I manage to make it work by adding to the top of bin/rails

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

AND in test_helper.rb, I set 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

I run tests with the command RAILS_ENV=test rails test

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!