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.

Max
  • 6,563
  • 4
  • 25
  • 33
ciembor
  • 7,189
  • 13
  • 59
  • 100
  • 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 flag – equivalent8 Feb 22 '16 at 11:44

2 Answers2

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

ahmet
  • 4,955
  • 11
  • 39
  • 64
  • 1
    the request was for rspec, but devise's tests don't use rspec. – lordB8r Dec 08 '14 at 18:38
  • 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) – equivalent8 Feb 22 '16 at 11:22
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
kmanzana
  • 1,138
  • 1
  • 13
  • 23
  • careful doh, devise mailer has own mailer by default, try `Devise.mailer.deliveries` instead of `ActionMailer::Base.deliveries`) if you notice side effects – equivalent8 Feb 22 '16 at 11:29