[英]Rails Rspec - model attribute undefined method `>' for nil:NilClass
I keep getting the error undefined method `>' for nil:NilClass when I run my rspec on the Billing model. 当我在Billing模型上运行rspec时,我不断得到nil:NilClass的错误未定义方法`>'。 Below is my code for the model
下面是我的模型代码
class Billing < ActiveRecord::Base
validate :start_date, :end_date, presence: true
validate :is_valid_date?
def is_valid_date?
errors.add(:start_date, 'must be in the past') if start_date > Time.current.to_date
errors.add(:end_date, 'must be in the past') if end_date > Time.current.to_date
errors.add(:end_date, 'must be more than or equal to start date') if start_date > end_date
end
end
And this is my code for the rspec 这是我的rspec代码
require 'rails_helper'
RSpec.describe FamilyBilling, type: :model do
it { should validate_presence_of(:start_date) }
it { should validate_presence_of(:end_date) }
it { should validate_presence_of(:amount) }
it { should validate_presence_of(:hours) }
it { should validate_presence_of(:rate) }
it { should validate_presence_of(:processing_fee) }
it { should_not validate_presence_of(:tip) }
end
I get this error when I run rspec 运行rspec时出现此错误
Failed examples:
rspec ./spec/models/billing_spec.rb:8 # Billing should require start_date to be set
rspec ./spec/models/billing_spec.rb:9 # Billing should require end_date to be set
rspec ./spec/models/billing_spec.rb:10 # Billing should require amount to be set
rspec ./spec/models/billing_spec.rb:11 # Billing should require hours to be set
rspec ./spec/models/billing_spec.rb:12 # Billing should require rate to be set
rspec ./spec/models/billing_spec.rb:13 # Billing should require processing_fee to be set
rspec ./spec/models/billing_spec.rb:14 # Billing should not require tip to be set
and all of them show this error 他们都显示此错误
Failure/Error: errors.add(:start_date, 'must be in the past') if start_date > Time.current.to_date
NoMethodError:
undefined method `>' for nil:NilClass
What did I do wrong? 我做错了什么?
Your custom validator expects that both start_date
and end_date
must exist. 您的自定义验证程序期望
start_date
和end_date
必须存在。 If they don't exist that error is thrown - for example here: start_date > Time.current.to_date
. 如果不存在,则会引发错误-例如,在这里:
start_date > Time.current.to_date
。
Therefore you should validate their presence explicitly and check that they are present in your custom validator: 因此,您应该显式验证其存在,并检查它们是否存在于您的自定义验证器中:
class Billing < ActiveRecord::Base
validates :start_date, :end_date, presence: true
validate :dates_valid?
def dates_valid?
errors.add(:start_date, 'must be in the past') if start_date && start_date > Time.current.to_date
errors.add(:end_date, 'must be in the past') if end_date && end_date > Time.current.to_date
errors.add(:end_date, 'must be more than or equal to start date') if start_date && end_date && start_date > end_date
end
end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.