當我在結算模型上運行我的rspec時,我總是收到nil:NilClass的錯誤未定義方法`>'。下面是我對模型Rails Rspec - model屬性undefined方法`>'爲零:NilClass
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
代碼,這是我的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
我得到當我運行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
,他們都表示這此錯誤代碼錯誤
Failure/Error: errors.add(:start_date, 'must be in the past') if start_date > Time.current.to_date
NoMethodError:
undefined method `>' for nil:NilClass
我做了什麼做錯了?
哪條線?包含整個錯誤,所以我們不必猜測 –