Back to Repositories

Testing Devise Mailer Configuration and Content Handling in heartcombo/devise

This test suite validates the Devise mailer functionality, focusing on email configuration and content handling. It ensures proper behavior of mail delivery options and default value processing in the authentication system.

Test Coverage Overview

The test suite covers essential mailer functionality in Devise, particularly focusing on mail configuration and content encoding.

Key areas tested include:
  • Block handling in mail method calls
  • Content transfer encoding settings
  • Default value processing with proc definitions
  • Email header configuration (from, reply-to)

Implementation Analysis

The testing approach uses ActionMailer::TestCase for mailer validation, implementing isolated test cases for specific functionality. It employs subclassing of Devise::Mailer to test customized mailer behavior and validates both simple and complex mail configurations with proc-based defaults.

Technical Details

Testing tools and configuration:
  • ActionMailer::TestCase framework
  • Devise::Mailer as base class
  • Custom TestMailer implementations
  • Minitest assertions
  • User factory helpers (create_user)

Best Practices Demonstrated

The test suite demonstrates several testing best practices including isolation of test cases, proper subclassing for specific test scenarios, and comprehensive validation of mail attributes. It shows effective use of Ruby blocks, proc handling, and proper separation of concerns in mailer testing.

heartcombo/devise

test/mailers/mailer_test.rb

            
# frozen_string_literal: true

require "test_helper"

class MailerTest < ActionMailer::TestCase
  test "pass given block to #mail call" do
    class TestMailer < Devise::Mailer
      def confirmation_instructions(record, token, opts = {})
        @token = token
        devise_mail(record, :confirmation_instructions, opts) do |format|
          format.html(content_transfer_encoding: "7bit")
        end
      end
    end

    mail = TestMailer.confirmation_instructions(create_user, "confirmation-token")

    assert mail.content_transfer_encoding, "7bit"
  end

  test "default values defined as proc with different arity are handled correctly" do
    class TestMailerWithDefault < Devise::Mailer
      default from: -> { computed_from }
      default reply_to: ->(_) { computed_reply_to }

      def confirmation_instructions(record, token, opts = {})
        @token = token
        devise_mail(record, :confirmation_instructions, opts)
      end

      private

      def computed_from
        "[email protected]"
      end

      def computed_reply_to
        "[email protected]"
      end
    end

    mail = TestMailerWithDefault.confirmation_instructions(create_user, "confirmation-token")
    assert mail.from, "[email protected]"
    assert mail.reply_to, "[email protected]"
  end
end