I have this factory
FactoryGirl.define do
factory :gst_report do
association :organisation, factory: :active_organisation
period_months 3
period_start_date Date.parse('2015-01-01')
period_end_date Date.parse('2015-02-01')
end
end
But in the tests period_start_date
is often set to other dates. For each of these tests, I want period_end_date
to simply be a month after period_start_date
.
Is it possible to make period_end_date
a method that gets the current period_start_date
and adds one month to it?
Yes, you can set the period_end_date
to period_start_date
plus a month by using FactoryGirl Callbacks
This should work for your case:
FactoryGirl.define do
factory :gst_report do
association :organisation, factory: :active_organisation
period_months 3
period_start_date Date.parse('2015-01-01')
after(:build) do |gst_report|
gst_report.period_end_date = (gst_report.period_start_date + 1.month) unless gst_report.period_end_date
end
end
end
Note that period_end_date
is not specified before hand. We're also only assigning the end date if it hasn't been specified when building the factory so that you can still specify different start/end periods through your specs.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments