Search code examples
ruby-on-rails-4testingrspecfactory-bot

FactoryGirl set updated_at attribute in factory


Using FactoryGirl 4.7.0, I have a factory that I want to specify the updated_at attribute to a date in the past.

factory :living_arrangement do

    trait :expired do
        updated_at Date.new(2012, 3, 6)
        # or
        updated_at { Date.new(2012, 3, 6) }
    end
end

However these are not working. The factory updated_at attribute keeps getting set back to Date.now.

How do I get this to work?


Solution

  • Turns out I had to use a callback for when the factory gets created, like so

    factory :living_arrangement do
        trait :expired do
            after :create do |living|
                living.updated_at = Date.new(2012, 3, 6)
            end
        end
    end