我决定尝试使用simplecov gem.我认为这是很酷的工具,但我有一个问题:
我有模型用户,我有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
Run Code Online (Sandbox Code Playgroud)
user_spec.rb
require 'rails_helper'
RSpec.describe User, type: :model do
describe '#validations' do
it { should have_one(:profile).dependent(:destroy) }
it …Run Code Online (Sandbox Code Playgroud) 我需要通过整数值获取枚举状态的字符串名称,然后我会这样做
Order.states.find{|x| x[1] == data['stateId']}
Run Code Online (Sandbox Code Playgroud)
有人知道更好的方法吗?
enum state: {
created: 0,
cancelled: 100,
complete: 10,
}
Run Code Online (Sandbox Code Playgroud) 我有下一个问题.当我尝试使用allow_any_instance_of来存储ActiveRecord模型的实例方法时,我收到错误消息"模型没有实现#method",但是如果我发送实际请求到数据库创建或选择此模型的实例对象之前,我不有这个消息,一切都好!我在控制台中遇到同样的问题
Reloading...
>> Search.method_defined?(:tickets_count)
=> false
>> Search.method_defined?(:affiliate_id)
=> false
>> Search.last
Search Load (3.9ms) SELECT "searches".* FROM "searches" ORDER BY "searches"."id" DESC LIMIT 1
=> #<Search:0x007fe2aecf2900
id: 515711,
tickets_count: 1,
affiliate_id: nil
>> Search.method_defined?(:affiliate_id)
=> true
>> Search.method_defined?(:tickets_count)
=> true
Run Code Online (Sandbox Code Playgroud)
有人可以解释一下,到底发生了什么事,拜托?))
我正在使用RSpec(Rails 4.2.5 上的3.4 )测试视图。我在CRUD控制器中使用了decent_exposure gem。decent_exposure使得定义命名方法变得容易,这些命名方法可用于我的视图并记住结果值。
但是我不明白如何在视图规范中添加这些方法。我尝试根据RSpec文档进行操作,但是会引发错误。
为什么不起作用?如何article在视图中存根方法?
我的控制器
class Account::ArticlesController < Account::BaseController
expose(:articles) { current_user.articles }
expose(:article, attributes: :article_params)
# POST /account/articles
# POST /account/articles.json
def create
respond_to do |format|
if article.save
format.html { redirect_to account_article_url(article), notice: 'Article was successfully created.' }
format.json { render :show, status: :created, location: account_article_url(article) }
else
format.html { render :new }
format.json { render json: article.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /account/articles/1
# …Run Code Online (Sandbox Code Playgroud)