Rspe c 'validates_numericity_o'f тест не пройден с использованием FactoryBot и странной ошибки - PullRequest
0 голосов
/ 20 апреля 2020

Я пытаюсь внедрить больше тестов в приложениях Rails наших компаний и использую Rspe c, FactoryBot и Matcha соответствия. Основы легко реализовать, например, ассоциации и наличие. Это более «exoti c», где я сталкиваюсь с проблемами и получаю ошибки, которые я не совсем понимаю, как исправить, поскольку FactoryBot создает хорошие экземпляры для тестирования. Вот моя модель, которую я тестирую:

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

Вот файл FactoryBot:

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

, а вот фактический тест RSpe c:


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

Я получаю ошибку:


  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

1 Ответ

0 голосов
/ 20 апреля 2020

validates_numericality не имеет отступов для атрибутов даты / времени. Существуют отдельные гемы, такие как validates_timelines , которые реализуют проверки для дат, или вы можете написать свои собственные проверки.

class Outage
  validate :end_time_is_after_start

  private
  def end_time_is_after_start
    errors.add(:end_time, 'is not after start time') unless end_time > start_time
  end
end

Лучший способ обработать время на вашем предприятии - использовать зависимые атрибуты. :

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

Это гарантирует, что end_time всегда будет относительно start_time.

...