Skip to content
Gasv0.10.0

gas/email

email/emailtest

import "github.com/gasmod/gas/email/emailtest"

Package emailtest holds a mock of gas.EmailProvider, MockEmail, for tests of code that sends email.

The mock records each call, and returns what the function in the field named after the method returns:

m := &emailtest.MockEmail{
SendFn: func(context.Context, *gas.Email) error { return errBounce },
}
err := m.Send(ctx, &gas.Email{To: []string{"alice@example.com"}})
// err is errBounce, and m.CallCount("Send") is 1

The mock keeps none of the email package’s rules: it checks no address, renders no template, and sends nothing. To test against those rules without a server, use gas/email/memory, whose outbox holds what SES would be sent.

With a field nil, its method succeeds: Send, SendFromTemplate and CheckReady return nil.

Each method appends a Call with its name and its arguments after the context, before it calls its field. The arguments:

  • Send: a copy of the *gas.Email.
  • SendFromTemplate: a copy of the *gas.TemplatedEmail.
  • Either, for a nil message: nil.
  • CheckReady: nothing.

A copy shares no slice or map with the caller’s message, so a caller that reuses the message does not change a recorded call. The Data of a TemplatedEmail is the same value, not a copy.

To read the record:

The methods may be called from several goroutines at once, as the calls are kept under a lock, which Calls, CallCount and Reset take too. Setting a field while a method runs is a data race.

type Call struct {
// Method is the name of the method called.
Method string
// Args are the method's arguments after the context, in order.
Args []any
}

Call is one call of the mock’s method.

type MockEmail struct {
// SendFn is what Send returns. When it is nil, Send returns nil.
SendFn func(ctx context.Context, msg *gas.Email) error
// SendFromTemplateFn is what SendFromTemplate returns. When it is nil,
// SendFromTemplate returns nil.
SendFromTemplateFn func(ctx context.Context, msg *gas.TemplatedEmail) error
// CheckReadyFn is what CheckReady returns. When it is nil, CheckReady
// returns nil.
CheckReadyFn func(ctx context.Context) error
// contains filtered or unexported fields
}

MockEmail is a gas.EmailProvider and gas.ReadyReporter that records its calls, as described in the package documentation.

func (m *MockEmail) CallCount(method string) int

CallCount returns how many calls were made to method.

func (m *MockEmail) Calls() []Call

Calls returns a deep copy of the calls, in the order they were made: changing a message it returns changes no recorded call.

func (m *MockEmail) CheckReady(ctx context.Context) error

CheckReady records the call and returns CheckReadyFn’s result.

func (m *MockEmail) Reset()

Reset forgets every call.

func (m *MockEmail) Send(ctx context.Context, msg *gas.Email) error

Send records the call and returns SendFn’s result.

func (m *MockEmail) SendFromTemplate(ctx context.Context, msg *gas.TemplatedEmail) error

SendFromTemplate records the call and returns SendFromTemplateFn’s result.