Testing (RSpec) with Devise's confirmable module

后端 未结 7 1087
不思量自难忘°
不思量自难忘° 2020-12-24 13:06

It\'s my understanding that Rails\' testing environment is torn down and rebuilt before each test...so how do I test a controller that requires that a user be logged in and

相关标签:
7条回答
  • 2020-12-24 13:39

    For fabrication gem it is

    Fabricator(:user) do
      after_build { |user| user.confirm! }
    end
    
    0 讨论(0)
  • 2020-12-24 13:40

    for latest FactoryGirl version:

    FactoryGirl.define do
    
      factory :confirmed_user, :parent => :user do
        after(:create) { |user| user.confirm! }
      end
    
    end
    
    0 讨论(0)
  • 2020-12-24 13:40

    Try stubbing active?

    @user = Factory.create(:user)
    @user.stub(:active?).and_return(true)
    sign_in @user
    

    You might also need to stub confirmed?, I'm not too familiar with Devise.

    0 讨论(0)
  • 2020-12-24 13:52

    If you are using factory_girl to generate your models, you can use after_create to confirm each new user.

    Factory.define :confirmed_user, :parent => :user do |f|
      f.after_create { |user| user.confirm! }
    end
    
    0 讨论(0)
  • 2020-12-24 13:58

    For Devise 4.2 it's now confirm (not confirm!).

    Source

    4.2.0 - 2016-07-01

    Remove the Devise::Models::Confirmable#confirm! method, use confirm instead.

    Code below also prevents mailers from being generated thanks to after(:build) vs after(:create) for setting confirmed_at

    FactoryGirl.define do
      factory :user do
        after(:build)   { |u| u.skip_confirmation_notification! }
        after(:create)  { |u| u.confirm }
        ...
      end
    end
    
    0 讨论(0)
  • 2020-12-24 14:03

    In your test environment ActionMailer::Base.delivery_method should be set to :test, which means that these emails will not be sent out. If this setting is set to something else such as smtp by way of a configuration in say config/environments.rb, then emails will be sent out.

    If that setting's already there, then to use the User object (as in, to be actually able to log in) you'll need to call confirm! on it:

    user = User.first
    user.confirm!
    
    0 讨论(0)
提交回复
热议问题