RSpec and testing Devise's mailer
Asked Answered
N

2

8

I had some issues with sending confirmation emails in Devise. That's why I would like to write tests for this functionality. How could I do this, when I don't create my own mailers?

EDIT

I decided, that this should be enough:

it 'should send an email' do
  user
  put :complete, params
  user.send(:send_confirmation_notification?).should == true
end

Please, let me know if I missed something.

Naumachia answered 4/6, 2013 at 11:2 Comment(1)
For googling people: App I'm currently working at has Devise 3.5.2 and emails are being stubbed. Not sure if this is Devise default for test environment or I've just missed some local configuration. What I'm trying to say that sometimes you may deal with a case where it's not possible to test Devise.mailer.deliveries.size or ActionMailer::Base.deliveries.size as they are always 0 therefore only way how to test this is with user.send_confirmation_notification? like example in the question. Careful doh ! don't use user.confirmed_at? as user.skip_confirmation! is setting this flagNeomineomycin
D
4

Have you looked at the tests which have been written for Devise?

https://github.com/plataformatec/devise/blob/master/test/mailers/confirmation_instructions_test.rb

Dowry answered 4/6, 2013 at 11:4 Comment(2)
the request was for rspec, but devise's tests don't use rspec.Clisthenes
doesn't matter if it's RSpec, MiniTest or Test::Unit, the concept and what messages to look for during testing is important ( +1 on the answer)Neomineomycin
M
3

This worked for me if you want to have a more explicit test and actually test the email is sent with RSpec.

it 'sends a confirmation email' do
  user = FactoryGirl.build :user
  expect { user.save }.to change(ActionMailer::Base.deliveries, :count).by(1)
end
Methylamine answered 6/10, 2015 at 20:30 Comment(1)
careful doh, devise mailer has own mailer by default, try Devise.mailer.deliveries instead of ActionMailer::Base.deliveries) if you notice side effectsNeomineomycin

© 2022 - 2024 — McMap. All rights reserved.