mocksmtp

package module
v1.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 5, 2019 License: MIT Imports: 3 Imported by: 2

README

MockSMTP

MockSMTP is an in-memory implementation for buffalo Sender interface. It's intended to catch sent messages for test purposes.

Usage

mailers/mailers_test.go:

package mailers_test

import (
	"github.com/stanislas-m/mocksmtp"
	"gitlab.com/stanislas-m/mybuffaloproject/mailers"
)

func init() {
	mailers.SMTP = mocksmtp.New()
}

mailers/registration_mail.go:

package mailers

import (
	"github.com/gobuffalo/buffalo/mail"
	"github.com/gobuffalo/buffalo/render"
	"github.com/pkg/errors"
)

func SendRegistrationMail(email string) error {
	m := mail.NewMessage()

	m.Subject = "Registration succeed"
	m.From = "Buffalo <noreply@gobuffalo.io>"
	m.To = []string{email}
	err := m.AddBody(r.HTML("registration_mail.html"), render.Data{})
	if err != nil {
		return errors.WithStack(err)
	}
	if err := SMTP.Send(m); err != nil {
		return errors.WithStack(err)
	}
	return nil
}

mailers/registration_mail_test.go:

package mailers_test

import (
	"testing"

	"github.com/stretchr/testify/require"
	"gitlab.com/stanislas-m/mybuffaloproject/mailers"
)

func Test_SendRegistrationMail(t *testing.T) {
    r := require.New(t)
    ms, v := mailers.SMTP.(*mocksmtp.MockSMTP)
    r.True(v)
    // Clear SMTP queue after test
    defer ms.Clear()
    err := mailers.SendRegistrationMail("test@gobuffalo.io")
    r.NoError(err)
    // Test email
    r.Equal(1, ms.Count())
    m, err := ms.LastMessage()
    r.NoError(err)
    r.Equal("Buffalo <noreply@gobuffalo.io>", m.From)
    r.Equal("Registration succeed", m.Subject)
    r.Equal(1, len(m.To))
    r.Equal("test@gobuffalo.io", m.To[0])
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrNoMessage = errors.New("no message sent")

ErrNoMessage is returned when no message was caught.

Functions

This section is empty.

Types

type MockSMTP

type MockSMTP struct {
	// contains filtered or unexported fields
}

MockSMTP is an in-memory implementation for buffalo `Sender` interface. It's intended to catch sent messages for test purposes.

func New

func New() *MockSMTP

New constructs a new MockSMTP.

func (*MockSMTP) Clear

func (s *MockSMTP) Clear()

Clear destroys all sent messages.

func (*MockSMTP) Count

func (s *MockSMTP) Count() int

Count gets the amount of sent messages.

func (*MockSMTP) LastMessage

func (s *MockSMTP) LastMessage() (mail.Message, error)

LastMessage gets the last sent message, if it exists. It returns `NoMessage` error if there is not last message.

func (*MockSMTP) Messages

func (s *MockSMTP) Messages() []mail.Message

Messages gets the list of sent messages.

func (*MockSMTP) Send

func (s *MockSMTP) Send(m mail.Message) error

Send implements buffalo `Sender` interface, to send mails.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL