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

# template/templatetest

> Package templatetest holds a mock of gas.TemplateProvider, MockTemplate, for tests of code that reads or registers templates.

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

```go
import "github.com/gasmod/gas/template/templatetest"
```

Package templatetest holds a mock of gas.TemplateProvider,
[MockTemplate](#MockTemplate), for tests of code that reads or registers templates.

## Usage

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

```go
m := &templatetest.MockTemplate{
	GetFn: func(context.Context, string) ([]byte, error) {
		return []byte("<p>hi</p>"), nil
	},
}
b, err := m.Get(ctx, "home.html")
// b is <p>hi</p>, err is nil, and m.CallCount("Get") is 1
```

## Defaults

With a field nil, its method behaves as an empty store:

- Get returns an error wrapping template.ErrTemplateNotFound,
`template: not found:` and the quoted name.
- List returns an empty, non-nil list.
- Register and RegisterFS 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 recorded:

- Get: the name.
- List: none.
- Register: the name and the content. The content is recorded as a
copy, nil for nil, so that a caller that reuses its slice does not
change a recorded call.
- RegisterFS: the fs.FS.

To read the record:

- Calls returns a copy of the calls in the order they were made.
- CallCount counts those of one method.
- Reset forgets them all.

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="MockTemplate"></a>

### type MockTemplate

```go
type MockTemplate struct {
	// GetFn is what Get returns. When it is nil, Get returns an error
	// wrapping template.ErrTemplateNotFound.
	GetFn func(ctx context.Context, name string) ([]byte, error)
	// ListFn is what List returns. When it is nil, List returns an empty
	// list.
	ListFn func(ctx context.Context) ([]string, error)
	// RegisterFn is what Register returns. When it is nil, Register
	// returns nil.
	RegisterFn func(ctx context.Context, name string, content []byte) error
	// RegisterFSFn is what RegisterFS returns. When it is nil, RegisterFS
	// returns nil.
	RegisterFSFn func(ctx context.Context, fsys fs.FS) error
	// contains filtered or unexported fields
}
```

MockTemplate is a gas.TemplateProvider that records its calls, as
described in the package documentation.

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

#### func (*MockTemplate) CallCount

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

CallCount returns how many calls were made to method.

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

#### func (*MockTemplate) Calls

```go
func (m *MockTemplate) Calls() []Call
```

Calls returns a copy of the calls, in the order they were made.

<a id="MockTemplate.Get"></a>

#### func (*MockTemplate) Get

```go
func (m *MockTemplate) Get(ctx context.Context, name string) ([]byte, error)
```

Get records the call and returns GetFn's result.

<a id="MockTemplate.List"></a>

#### func (*MockTemplate) List

```go
func (m *MockTemplate) List(ctx context.Context) ([]string, error)
```

List records the call and returns ListFn's result.

<a id="MockTemplate.Register"></a>

#### func (*MockTemplate) Register

```go
func (m *MockTemplate) Register(ctx context.Context, name string, content []byte) error
```

Register records the call and returns RegisterFn's result.

<a id="MockTemplate.RegisterFS"></a>

#### func (*MockTemplate) RegisterFS

```go
func (m *MockTemplate) RegisterFS(ctx context.Context, fsys fs.FS) error
```

RegisterFS records the call and returns RegisterFSFn's result.

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

#### func (*MockTemplate) Reset

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

Reset forgets every call.
