【发布时间】: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.pry或puts "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