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 1The 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.
Defaults
Section titled “Defaults”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:
- MockEmail.Calls returns a deep copy of the calls in the order they were made, so that changing what it returns does not change them either.
- MockEmail.CallCount counts those of one method.
- MockEmail.Reset forgets them all.
Concurrency
Section titled “Concurrency”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.
API reference
Section titled “API reference”type Call
Section titled “type Call”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
Section titled “type MockEmail”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 (*MockEmail) CallCount
Section titled “func (*MockEmail) CallCount”func (m *MockEmail) CallCount(method string) intCallCount returns how many calls were made to method.
func (*MockEmail) Calls
Section titled “func (*MockEmail) Calls”func (m *MockEmail) Calls() []CallCalls returns a deep copy of the calls, in the order they were made: changing a message it returns changes no recorded call.
func (*MockEmail) CheckReady
Section titled “func (*MockEmail) CheckReady”func (m *MockEmail) CheckReady(ctx context.Context) errorCheckReady records the call and returns CheckReadyFn’s result.
func (*MockEmail) Reset
Section titled “func (*MockEmail) Reset”func (m *MockEmail) Reset()Reset forgets every call.
func (*MockEmail) Send
Section titled “func (*MockEmail) Send”func (m *MockEmail) Send(ctx context.Context, msg *gas.Email) errorSend records the call and returns SendFn’s result.
func (*MockEmail) SendFromTemplate
Section titled “func (*MockEmail) SendFromTemplate”func (m *MockEmail) SendFromTemplate(ctx context.Context, msg *gas.TemplatedEmail) errorSendFromTemplate records the call and returns SendFromTemplateFn’s result.