Use dynamic times and dates in factories.

The tests depending on the date changing were still failing because
Date.current was being stubbed after loading the factories. The
following lines affected these specific tests:

factory :poll_officer_assignment, class: 'Poll::OfficerAssignment' do
(...)
  date Date.current
end

So if the tests were executed right before midnight, the sequence was:

1. The factories file was loaded, assigning Date.current to the date of
every Poll::OfficerAssignment to be created.
2. Time passed, so now it was after midnight.
3. The `travel_to` method freezed time, after midnight.
4. A Poll::OfficerAssignment factory was created, using the date it was
before midnight.

Using dynamic fixtures solves the problem:

factory :poll_officer_assignment, class: 'Poll::OfficerAssignment' do
(...)
  date { Date.current }
end

Now the sequence is:

1. The factories file is loaded, and since it finds a block, doesn't
assign a static value to every Poll::OfficerAssignment to be created.
2. Time passes, so now it's after midnight.
3. The `travel_to` method freezes time, after midnight.
4. A Poll::OfficerAssignment factory was created, and in executes the
block, using the current date, that is, after midnight.
This commit is contained in:
Javier Martín
2018-07-07 00:08:20 +02:00
parent de0afe1621
commit 0cf799add6

View File

@@ -24,7 +24,7 @@ FactoryBot.define do
end end
trait :level_two do trait :level_two do
residence_verified_at Time.current residence_verified_at { Time.current }
unconfirmed_phone "611111111" unconfirmed_phone "611111111"
confirmed_phone "611111111" confirmed_phone "611111111"
sms_confirmation_code "1234" sms_confirmation_code "1234"
@@ -36,28 +36,28 @@ FactoryBot.define do
end end
trait :level_three do trait :level_three do
verified_at Time.current verified_at { Time.current }
document_type "1" document_type "1"
document_number document_number
end end
trait :hidden do trait :hidden do
hidden_at Time.current hidden_at { Time.current }
end end
trait :with_confirmed_hide do trait :with_confirmed_hide do
confirmed_hide_at Time.current confirmed_hide_at { Time.current }
end end
trait :verified do trait :verified do
residence_verified_at Time.current residence_verified_at { Time.current }
verified_at Time.current verified_at { Time.current }
end end
trait :in_census do trait :in_census do
document_number "12345678Z" document_number "12345678Z"
document_type "1" document_type "1"
verified_at Time.current verified_at { Time.current }
end end
end end
@@ -112,7 +112,7 @@ FactoryBot.define do
factory :lock do factory :lock do
user user
tries 0 tries 0
locked_until Time.current locked_until { Time.current }
end end
factory :verified_user do factory :verified_user do
@@ -127,15 +127,15 @@ FactoryBot.define do
association :author, factory: :user association :author, factory: :user
trait :hidden do trait :hidden do
hidden_at Time.current hidden_at { Time.current }
end end
trait :with_ignored_flag do trait :with_ignored_flag do
ignored_flag_at Time.current ignored_flag_at { Time.current }
end end
trait :with_confirmed_hide do trait :with_confirmed_hide do
confirmed_hide_at Time.current confirmed_hide_at { Time.current }
end end
trait :flagged do trait :flagged do
@@ -173,15 +173,15 @@ FactoryBot.define do
association :author, factory: :user association :author, factory: :user
trait :hidden do trait :hidden do
hidden_at Time.current hidden_at { Time.current }
end end
trait :with_ignored_flag do trait :with_ignored_flag do
ignored_flag_at Time.current ignored_flag_at { Time.current }
end end
trait :with_confirmed_hide do trait :with_confirmed_hide do
confirmed_hide_at Time.current confirmed_hide_at { Time.current }
end end
trait :flagged do trait :flagged do
@@ -369,8 +369,8 @@ FactoryBot.define do
kind :balloting kind :balloting
summary Faker::Lorem.sentence(3) summary Faker::Lorem.sentence(3)
description Faker::Lorem.sentence(10) description Faker::Lorem.sentence(10)
starts_at Date.yesterday starts_at { Date.yesterday }
ends_at Date.tomorrow ends_at { Date.tomorrow }
enabled true enabled true
end end
@@ -414,7 +414,7 @@ FactoryBot.define do
association :status, factory: :budget_investment_status association :status, factory: :budget_investment_status
sequence(:title) { |n| "Budget investment milestone #{n} title" } sequence(:title) { |n| "Budget investment milestone #{n} title" }
description 'Milestone description' description 'Milestone description'
publication_date Date.current publication_date { Date.current }
end end
factory :vote do factory :vote do
@@ -467,15 +467,15 @@ FactoryBot.define do
sequence(:body) { |n| "Comment body #{n}" } sequence(:body) { |n| "Comment body #{n}" }
trait :hidden do trait :hidden do
hidden_at Time.current hidden_at { Time.current }
end end
trait :with_ignored_flag do trait :with_ignored_flag do
ignored_flag_at Time.current ignored_flag_at { Time.current }
end end
trait :with_confirmed_hide do trait :with_confirmed_hide do
confirmed_hide_at Time.current confirmed_hide_at { Time.current }
end end
trait :flagged do trait :flagged do
@@ -601,7 +601,7 @@ FactoryBot.define do
factory :poll_officer_assignment, class: 'Poll::OfficerAssignment' do factory :poll_officer_assignment, class: 'Poll::OfficerAssignment' do
association :officer, factory: :poll_officer association :officer, factory: :poll_officer
association :booth_assignment, factory: :poll_booth_assignment association :booth_assignment, factory: :poll_booth_assignment
date Date.current date { Date.current }
trait :final do trait :final do
final true final true
@@ -611,7 +611,7 @@ FactoryBot.define do
factory :poll_shift, class: 'Poll::Shift' do factory :poll_shift, class: 'Poll::Shift' do
association :booth, factory: :poll_booth association :booth, factory: :poll_booth
association :officer, factory: :poll_officer association :officer, factory: :poll_officer
date Date.current date { Date.current }
trait :vote_collection_task do trait :vote_collection_task do
task 0 task 0
@@ -669,7 +669,7 @@ FactoryBot.define do
year_of_birth "1980" year_of_birth "1980"
trait :invalid do trait :invalid do
year_of_birth Time.current.year year_of_birth { Time.current.year }
end end
end end
@@ -679,11 +679,11 @@ FactoryBot.define do
sequence(:name) { |n| "org#{n}" } sequence(:name) { |n| "org#{n}" }
trait :verified do trait :verified do
verified_at Time.current verified_at { Time.current }
end end
trait :rejected do trait :rejected do
rejected_at Time.current rejected_at { Time.current }
end end
end end
@@ -702,13 +702,13 @@ FactoryBot.define do
factory :ahoy_event, class: Ahoy::Event do factory :ahoy_event, class: Ahoy::Event do
id { SecureRandom.uuid } id { SecureRandom.uuid }
time DateTime.current time { DateTime.current }
sequence(:name) {|n| "Event #{n} type"} sequence(:name) {|n| "Event #{n} type"}
end end
factory :visit do factory :visit do
id { SecureRandom.uuid } id { SecureRandom.uuid }
started_at DateTime.current started_at { DateTime.current }
end end
factory :campaign do factory :campaign do
@@ -721,7 +721,7 @@ FactoryBot.define do
association :notifiable, factory: :proposal association :notifiable, factory: :proposal
trait :read do trait :read do
read_at Time.current read_at { Time.current }
end end
end end
@@ -741,8 +741,8 @@ FactoryBot.define do
style {["banner-style-one", "banner-style-two", "banner-style-three"].sample} style {["banner-style-one", "banner-style-two", "banner-style-three"].sample}
image {["banner.banner-img-one", "banner.banner-img-two", "banner.banner-img-three"].sample} image {["banner.banner-img-one", "banner.banner-img-two", "banner.banner-img-three"].sample}
target_url {["/proposals", "/debates" ].sample} target_url {["/proposals", "/debates" ].sample}
post_started_at Time.current - 7.days post_started_at { Time.current - 7.days }
post_ended_at Time.current + 7.days post_ended_at { Time.current + 7.days }
end end
factory :proposal_notification do factory :proposal_notification do
@@ -790,14 +790,14 @@ FactoryBot.define do
title "A collaborative legislation process" title "A collaborative legislation process"
description "Description of the process" description "Description of the process"
summary "Summary of the process" summary "Summary of the process"
start_date Date.current - 5.days start_date { Date.current - 5.days }
end_date Date.current + 5.days end_date { Date.current + 5.days }
debate_start_date Date.current - 5.days debate_start_date { Date.current - 5.days }
debate_end_date Date.current + 2.days debate_end_date { Date.current + 2.days }
draft_publication_date Date.current - 1.day draft_publication_date { Date.current - 1.day }
allegations_start_date Date.current allegations_start_date { Date.current }
allegations_end_date Date.current + 3.days allegations_end_date { Date.current + 3.days }
result_publication_date Date.current + 5.days result_publication_date { Date.current + 5.days }
debate_phase_enabled true debate_phase_enabled true
allegations_phase_enabled true allegations_phase_enabled true
draft_publication_enabled true draft_publication_enabled true
@@ -805,36 +805,36 @@ FactoryBot.define do
published true published true
trait :next do trait :next do
start_date Date.current + 2.days start_date { Date.current + 2.days }
end_date Date.current + 8.days end_date { Date.current + 8.days }
debate_start_date Date.current + 2.days debate_start_date { Date.current + 2.days }
debate_end_date Date.current + 4.days debate_end_date { Date.current + 4.days }
draft_publication_date Date.current + 5.days draft_publication_date { Date.current + 5.days }
allegations_start_date Date.current + 5.days allegations_start_date { Date.current + 5.days }
allegations_end_date Date.current + 7.days allegations_end_date { Date.current + 7.days }
result_publication_date Date.current + 8.days result_publication_date { Date.current + 8.days }
end end
trait :past do trait :past do
start_date Date.current - 12.days start_date { Date.current - 12.days }
end_date Date.current - 2.days end_date { Date.current - 2.days }
debate_start_date Date.current - 12.days debate_start_date { Date.current - 12.days }
debate_end_date Date.current - 9.days debate_end_date { Date.current - 9.days }
draft_publication_date Date.current - 8.days draft_publication_date { Date.current - 8.days }
allegations_start_date Date.current - 8.days allegations_start_date { Date.current - 8.days }
allegations_end_date Date.current - 4.days allegations_end_date { Date.current - 4.days }
result_publication_date Date.current - 2.days result_publication_date { Date.current - 2.days }
end end
trait :in_debate_phase do trait :in_debate_phase do
start_date Date.current - 5.days start_date { Date.current - 5.days }
end_date Date.current + 5.days end_date { Date.current + 5.days }
debate_start_date Date.current - 5.days debate_start_date { Date.current - 5.days }
debate_end_date Date.current + 1.day debate_end_date { Date.current + 1.day }
draft_publication_date Date.current + 1.day draft_publication_date { Date.current + 1.day }
allegations_start_date Date.current + 2.days allegations_start_date { Date.current + 2.days }
allegations_end_date Date.current + 3.days allegations_end_date { Date.current + 3.days }
result_publication_date Date.current + 5.days result_publication_date { Date.current + 5.days }
end end
trait :published do trait :published do