Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Testing (RSpec) with Devise's confirmable module

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 that user can't be created without Device's confirmable module getting in the way?

Devise's recommended method (below) creates a new user which is then sent an email by Devise's confirmable module. How do I get around this so I'm not 'creating' a user each time...or if I am I can get an object to test w/out "simulating" a new email for each spec?

 before(:each) do
    @user = Factory.create(:user)
    sign_in @user
  end

I'm sure I'm overlooking something painfully obvious as this must be a very common spec for anyone using Devise with confirmable...

like image 1000
Meltemi Avatar asked Dec 13 '10 21:12

Meltemi


2 Answers

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!
like image 81
Ryan Bigg Avatar answered Nov 09 '22 14:11

Ryan Bigg


for latest FactoryGirl version:

FactoryGirl.define do

  factory :confirmed_user, :parent => :user do
    after(:create) { |user| user.confirm }
  end

end
like image 19
enricostn Avatar answered Nov 09 '22 16:11

enricostn