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
With RubyMine, you can measure how much of your code is covered with tests using the SimpleCov analysis tool. You can run tests with coverage for any supported testing framework, analyze the percentage of covered files and lines in a separate tool window and editor, generate HTML reports, and so on.
True to its name, it's simple to install and use. Every time you run tests, you'll get an HTML-formatted report showing which lines of code in your app were run by one or more tests. From there, you can determine which areas of your code have sufficient coverage, and which areas need some work.
SimpleCov is a gem that measures code coverage for a test suite. I set up SimpleCov to measure coverage for our RSpec tests, but I noticed that the test coverage was not accurate whenever I ran RSpec with Spring.
SimpleCov is a code coverage analysis tool for Ruby.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With