I decided to try using simplecov gem. And I think it's cool tool, but I have one problem:
I have model User and I have user_spec.rb which contains test cases, but simplecov shows 0% coverage of this model. And It shows 100% coverage for other models, and It's true. I don't understand what's trouble with User model.
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
coverage
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
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
来源:https://stackoverflow.com/questions/35476814/simplecov-calculate-0-coverage-for-user-model