## Search

Page index: https://gasmod.dev/llms.txt. Add index.md to any page URL for its Markdown.

This site has a search endpoint: https://context7.com/api/v2/ask?siteKey=ask_b845b566cb63bd033e9c576d&query=<URL-encoded question>. It returns documentation that answers the question, with a source link for each part. No API key is needed. If nothing matches, it says so.

# email/emailtest

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

The Markdown of https://gasmod.dev/reference/email/emailtest/ from the Gas documentation, whose index for LLMs is https://gasmod.dev/llms.txt

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

Package emailtest holds a mock of gas.EmailProvider, [MockEmail](#MockEmail), for
tests of code that sends email.

## Usage

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

```go
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.

## Defaults

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

## Calls

Each method appends a [Call](#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](#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](#MockEmail.CallCount) counts those of one method.
- [MockEmail.Reset](#MockEmail.Reset) forgets them all.

## 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

<a id="Call"></a>

### type Call

```go
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.

<a id="MockEmail"></a>

### type MockEmail

```go
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.

<a id="MockEmail.CallCount"></a>

#### func (*MockEmail) CallCount

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

CallCount returns how many calls were made to method.

<a id="MockEmail.Calls"></a>

#### func (*MockEmail) Calls

```go
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.

<a id="MockEmail.CheckReady"></a>

#### func (*MockEmail) CheckReady

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

CheckReady records the call and returns CheckReadyFn's result.

<a id="MockEmail.Reset"></a>

#### func (*MockEmail) Reset

```go
func (m *MockEmail) Reset()
```

Reset forgets every call.

<a id="MockEmail.Send"></a>

#### func (*MockEmail) Send

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

Send records the call and returns SendFn's result.

<a id="MockEmail.SendFromTemplate"></a>

#### func (*MockEmail) SendFromTemplate

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

SendFromTemplate records the call and returns SendFromTemplateFn's
result.
