lundi 20 avril 2020

Rspec 'validates_numericality_o'f test failing using FactoryBot and strange error

I'm trying to implement more testing in our companies Rails apps and using Rspec, FactoryBot and Shoulda matchers to help. The basics are easy to implement like associations and presence_of. It's the more 'exotic' ones where I run into trouble and get errors that I don't quite understand how to fix given that FactoryBot is producing good instances for testing. Here's my Model i'm testing:

class Outage < ApplicationRecord
    belongs_to :service 

    validates_presence_of :start_time, :end_time, :reason, :frequency
    validates_numericality_of :start_time 
    validates_numericality_of :end_time, :is_greater_than => :start_time 
end

Here's the FactoryBot file:

FactoryBot.define do 
   factory :outage_fo, class: 'Outage' do 
        start_time { Time.now }
        end_time { Time.now + Random.rand(1e5..1e6).floor}
        is_recurring { false }
        frequency { "None" }
        reason { Faker::Company.bs }
        service_id { Random.rand(1..50) }
   end 

   factory :outage_ro, class: 'Outage' do 
        start_time { Time.now }
        end_time { Time.now + Random.rand(1e5..1e6).floor}
        is_recurring { true }
        frequency { ["Daily", "Monthly" ].sample }
        reason { Faker::Company.bs }
        service_id { Random.rand(1..50) }
   end 
end

and here is the actual RSpec test:


require 'rails_helper'

RSpec.describe Outage, type: :model do
  let(:outage_fo) { build(:outage_fo) }


  describe "associations" do 
    it { should belong_to(:service) }
  end

  describe "validations" do 
    it { should validate_presence_of(:start_time) }
    it { should validate_presence_of(:end_time) }
    it { should validate_presence_of(:reason) }
    it { should validate_presence_of(:frequency) }
    it { should validate_numericality_of(:end_time).is_greater_than(Time.now) }
  end


end

The error I'm getting is:


  1) Outage validations is expected to validate that :end_time looks like a number greater than 2020-04-20 12:57:33 -0400
     Failure/Error: it { should validate_numericality_of(:end_time).is_greater_than(Time.now) }

       Expected Outage to validate that :end_time looks like a number greater
       than 2020-04-20 12:57:33 -0400, but this could not be proved.
         After setting :end_time to ‹"2020-04-20 12:57:33 -0400"› -- which was
         read back as ‹Mon, 20 Apr 2020 16:57:33 UTC +00:00› -- the matcher
         expected the Outage to be invalid and to produce the validation error
         "must be greater than 2020-04-20 12:57:33 -0400" on :end_time. The
         record was indeed invalid, but it produced these validation errors
         instead:

         * service: ["must exist"]
         * start_time: ["can't be blank"]
         * reason: ["can't be blank"]
         * end_time: ["is not a number"]

         As indicated in the message above, :end_time seems to be changing
         certain values as they are set, and this could have something to do
         with why this test is failing. If you've overridden the writer method
         for this attribute, then you may need to change it to make this test
         pass, or do something else entirely.
     # ./spec/models/outage_spec.rb:16:in `block (3 levels) in <top (required)>'

Finished in 0.24732 seconds (files took 1.87 seconds to load)
6 examples, 1 failure

Failed examples:

rspec ./spec/models/outage_spec.rb:16 # Outage validations is expected to validate that :end_time looks like a number greater than 2020-04-20 12:57:33 -0400

[12:57:33] (develop_2) tml_dashboard_2
// ♥ rspec spec/models/outage_spec.rb
.....F

Failures:

  1) Outage validations is expected to validate that :end_time looks like a number greater than 2020-04-20 12:58:31 -0400
     Failure/Error: it { should validate_numericality_of(:end_time).is_greater_than(Time.now) }

       Expected Outage to validate that :end_time looks like a number greater
       than 2020-04-20 12:58:31 -0400, but this could not be proved.
         After setting :end_time to ‹"2020-04-20 12:58:31 -0400"› -- which was
         read back as ‹Mon, 20 Apr 2020 16:58:31 UTC +00:00› -- the matcher
         expected the Outage to be invalid and to produce the validation error
         "must be greater than 2020-04-20 12:58:31 -0400" on :end_time. The
         record was indeed invalid, but it produced these validation errors
         instead:

         * service: ["must exist"]
         * start_time: ["can't be blank", "is not a number"]
         * reason: ["can't be blank"]
         * end_time: ["is not a number"]

         As indicated in the message above, :end_time seems to be changing
         certain values as they are set, and this could have something to do
         with why this test is failing. If you've overridden the writer method
         for this attribute, then you may need to change it to make this test
         pass, or do something else entirely.
     # ./spec/models/outage_spec.rb:16:in `block (3 levels) in <top (required)>'

Finished in 0.23808 seconds (files took 1.77 seconds to load)
6 examples, 1 failure

Aucun commentaire:

Enregistrer un commentaire