【问题标题】:devise FactoryGirl a user after its unconfirmation period在未确认期后将 FactoryGirl 设计为用户
【发布时间】:2017-10-25 15:04:49
【问题描述】:

使用 FactoryGirl 和设计,我如何在未确认期后构建/创建用户?

我首先尝试将:created_at 设置为几周前,然后我意识到设计'active_for_authentication 实际上基于confirmation_sent_at,但是将confirmation_sent_at 设置为几周前仍然没有帮助,因为在创建confirmation_sent_at 将设置为Time.now

我想了解此confirmation_sent_at 的设置时间/位置(我实际上还担心调用send_confirmation_instructions! 会重新发送邮件并重置此确认_sent_at`,但事实并非如此......)

但更重要的是,一个有效的 FactoryGirl 特征会在其未确认的访问期之后创建一个未确认的用户。

到目前为止,这是我的(不为未经证实的)工厂

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

结束 结束

【问题讨论】:

  • 也许你可以试试 Timecop 来冻结过去的时间进行创作?

标签: ruby-on-rails devise ruby-on-rails-5 devise-confirmable


【解决方案1】:

请记住,factory_girl 现在也是 factory_bot。我知道这并不能回答您的问题,但会有助于您继续前进。

【讨论】:

  • 由于这不是我的问题的答案,它应该保留作为对原始帖子的评论
猜你喜欢
  • 1970-01-01
  • 2021-04-22
  • 1970-01-01
  • 2023-04-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多