So I have an RSpec test that fails, I'm sure that the test is the issue as from a functionality perspective it works fine.
In short, end_time should not be before_start time. Upon saving the model this works correctly, only the RSpec is failing, any pointers would be greatly appreciated
chain_plan_spec.rb
# frozen_string_literal: true
# == Schema Information
#
# Table name: chain_plans
#
# id :bigint not null, primary key
# end_time :datetime
# start_time :datetime
# created_at :datetime not null
# updated_at :datetime not null
# faction_id :bigint not null
#
# Indexes
#
# index_chain_plans_on_faction_id (faction_id)
#
# Foreign Keys
#
# fk_rails_... (faction_id => factions.id)
#
require 'rails_helper'
RSpec.describe ChainPlan, type: :model do
it { is_expected.to validate_presence_of :start_time }
it { is_expected.to validate_presence_of :end_time }
it { is_expected.to validate_presence_of :faction }
it 'does not have a start date/time before an end date/time' do
cp = described_class.new
cp.start_time = Date.now
cp.end_time = cp.start - 1.minute
cp.save!
expect(cp).not_to be_valid
end
# TODO: end is 0 minute
# TODO start is 0 minute
end
chain_plan.rb
# frozen_string_literal: true
# == Schema Information
#
# Table name: chain_plans
#
# id :bigint not null, primary key
# end_time :datetime
# start_time :datetime
# created_at :datetime not null
# updated_at :datetime not null
# faction_id :bigint not null
#
# Indexes
#
# index_chain_plans_on_faction_id (faction_id)
#
# Foreign Keys
#
# fk_rails_... (faction_id => factions.id)
#
class ChainPlan < ApplicationRecord
belongs_to :faction
has_many :chain_plan_slots
validates :start_time, presence: { on: :create, message: "can't be blank" }
validates :end_time, presence: { on: :create, message: "can't be blank" }
validates :faction, presence: { on: :create, message: "can't be blank" }
validate :end_date_after_start_date?
private
def end_date_after_start_date?
return if end_time.blank? || start_time.blank?
errors.add(:end_date, 'must be after the start date') if end_time < start_time
end
end