I decided to try using simplecov gem, and I think it's a cool tool, but I have one problem:
I have a 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, which is true. I don't understand what's the issue with the 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