0
votes

Using FactoryGirl and devise, how can I build/create a user after its unconfirmation period ?

I have first tried to set the :created_at to some week ago then I realized devise' active_for_authentication was actually based on confirmation_sent_at, however setting confirmation_sent_at to some weeks ago still didn't help as during the creation the confirmation_sent_at would be set to Time.now

I'd like to understand when/where this confirmation_sent_at is set (I was actually also afraid that calling send_confirmation_instructions! would resent a mail AND reset this confirmation_sent_at` but this wasn't the case...)

But more importantly, a valid FactoryGirl trait that would create an unconfirmed user after its unconfirmed access period.

Here is my (not working for unconfirmed) Factory so far

FactoryGirl.define do
  factory :user do
    created_at        { Time.now }
    password          'qsdfghjklmù'
    confirmed_at      nil
    sequence(:email)  do |n|
      Faker::Internet.email(first_name.gsub(/[^\w]/, '_') + n.to_s)
    end

    transient do
      confirmed true
    end

    after(:build) do |user, evaluator|
      if evaluator.confirmed
        user.skip_confirmation!
        user.skip_reconfirmation!
      end
    end

    before(:create) do |user, evaluator|
      if evaluator.confirmed
        user.skip_confirmation!
        user.skip_reconfirmation!
      end
    end

    trait :unconfirmed do
      confirmed false
    end

    trait :after_unconfirmation_period do
      confirmation_sent_at do
        rand((User.allow_unconfirmed_access_for || 1.day).ago..10.days.ago)
      end
    end

end end

1
Maybe you could try Timecop to freeze the time in the past for creation?apneadiving

1 Answers

-3
votes

Keep in mind that factory_girl is now factory_bot too. I know this doesn't answer your question, but will be helpful for you moving forward.