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