简体   繁体   English

Rails Rspec-nil:NilClass的模型属性未定义方法“>”

[英]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_dateend_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.

相关问题 RSpec Rails中nil:NilClass的未定义方法&#39;session&#39; - undefined method `session' for nil:NilClass in RSpec Rails undefined方法`[]&#39;为nil:NilClass for Rails模型为new - undefined method `[]' for nil:NilClass for Rails model on new RSpec中nil:NilClass的未定义方法 - undefined method for nil:NilClass in RSpec RSPEC对nil:NilClass的未定义方法“ jobs”(红宝石在轨道上) - RSPEC undefined method 'jobs' for nil:NilClass (ruby on rails) 为什么nil:NilClass的rspec rails失败“未定义方法`destroy” - why is this failing 'undefined method `destroy' for nil:NilClass' rspec rails Rails RSpec测试:nil的未定义方法&#39;deliver&#39;:NilClass - Rails RSpec Testing: Undefined method 'deliver' for nil:NilClass Rails:RSpec-对于nil:NilClass的未定义方法`cookie_jar&#39; - Rails: RSpec - undefined method `cookie_jar' for nil:NilClass Rails 控制器 RSpec 未定义方法 `encode&#39; for nil:NilClass - Rails controller RSpec undefined method `encode' for nil:NilClass 带Rails 4.2的rspec的nil:NilClass的未定义方法`commit_records&#39; - rspec with Rails 4.2 undefined method `commit_records' for nil:NilClass Rspec/Rails3:NoMethodError:nil:NilClass 的未定义方法“文本” - Rspec/Rails3: NoMethodError: undefined method `text' for nil:NilClass
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM