Rails/RSpec/FactoryGirl—How to make a field point to a method?

Mirror318

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?

Navin Peiris

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

How to make the CommandBar point down in UWP

From Dev

How to make a point move along a vector?

From Dev

How to make this generic method?

From Dev

How to make `this` point to the outer class where the inner class has the same method name

From Dev

How to make custom field from django field

From Dev

How to make bitbucket web hook point to localhost?

From Dev

How to make a field required in Rails?

From Dev

How to make this method reusable?

From Dev

How to make a power point presentation like this

From Dev

How to make input text field dynamic expending with document.createElement("input") method?

From Dev

How to make SVG center float above point

From Dev

How to make select options in Rails that point to URLs?

From Dev

how to make every point display multiple data on every point with highcharts?

From Dev

how to make a multiple selection field?

From Dev

How to make the player always point to the mouse

From Dev

How to make an point move up and down

From Dev

How to make custom field from django field

From Dev

How to make a field only accessible from get and set method in django Model

From Dev

How to make /home point to another partition?

From Dev

How can I make a symbol point at the mouse?

From Dev

How to make dual screen crossover point upside

From Dev

how to make a method in asynctask

From Dev

how to make structs point to different strings?

From Dev

How to ignore field depending on method?

From Dev

How to make an input field into a reusable field?

From Dev

How to make the search field responsive?

From Dev

How to make an if statement point free

From Dev

Java: how to make field of names

From Dev

How to make a recursive method?

Related Related

HotTag

Archive