Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RSpec testing Devise Mailer

I am new to RSpec and TDD and I am having difficulties writing a RSpec test to test if Devise is actually sending the confirmation email after a user signs up. I know that my application is working as expected because I have physically tested the functionality in both development and production. However, I am still required to write the RSpec test for this functionality and I cannot figure out how to send a confirmation email through RSpec tests.

factories/user.rb

FactoryGirl.define do
  factory :user do
    name "Jack Sparrow"
    email { Faker::Internet.email }
    password "helloworld"
    password_confirmation "helloworld"
    confirmed_at Time.now
  end
end

spec/models/user_spec.rb

require 'rails_helper'

RSpec.describe User, type: :model do

  describe "user sign up" do
    before do
      @user = FactoryGirl.create(:user)
    end

    it "should save a user" do
      expect(@user).to be_valid
    end

    it "should send the user an email" do
      expect(ActionMailer::Base.deliveries.count).to eq 1
    end
  end
end

Why is Devise not sending a confirmation email after I create @user? My test returns ActionMailer::Base.deliveries.count = 0. As I said, I am new to RSpec and TDD so am I completely missing something here?

like image 323
country_dev Avatar asked Jun 16 '15 19:06

country_dev


1 Answers

Devise uses its own mailer, so try Devise.mailer.deliveries instead of ActionMailer::Base.deliveries if putting the test in the right controller's file doesn't work by itself.

like image 181
Joshua Grosso Reinstate CMs Avatar answered Nov 09 '22 17:11

Joshua Grosso Reinstate CMs