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

# config/configtest

> Package configtest holds MockConfig, a config.ConfigProvider for tests of code that reads configuration, and StaticProvider, a config.Provider that loads fixed values.

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

```go
import "github.com/gasmod/gas/config/configtest"
```

Package configtest holds [MockConfig](#MockConfig), a config.ConfigProvider for tests
of code that reads configuration, and [StaticProvider](#StaticProvider), a config.Provider
that loads fixed values.

## Usage

A MockConfig records each call, and calls the function in the field
named after the method, when it is set:

```go
m := &configtest.MockConfig{
	GetFn: func(key string) any { return "db" },
}
svc := NewService(m)
// m.CallCount("Get") is how many times svc called Get
```

[NewMockConfigWithValues](#NewMockConfigWithValues) sets every field to the methods of a real
config.Config holding the given values, so Get, Find, Bind and the rest
behave as they do in an app:

```go
m, err := configtest.NewMockConfigWithValues(map[string]any{
	"database": map[string]any{"host": "db"},
})
```

## Calls

Each method appends a [Call](#Call) with its name and arguments to Calls, then
calls its field. With the field nil, it returns zero values: nil, false,
or a nil error. LoadProviderContext records the provider but not the
context.

The methods may be called from several goroutines at once, as Calls is
appended to under a lock. [MockConfig.CallCount](#MockConfig.CallCount) and [MockConfig.Reset](#MockConfig.Reset)
take the lock too, but reading Calls directly does not, so read it once
the calls are done. Setting a field while a method runs is a data race.

## Values

NewMockConfigWithValues sets the values with config.Config.SetDefaults,
so their keys are trimmed and lowercased, and two keys that become the
same are an error. Nil values make an empty configuration. No provider
loads, so the environment does not reach it. Any field may be replaced
afterwards, and LoadProvider loads a provider into the configuration.

## Static provider

A [StaticProvider](#StaticProvider) gives a real config.Config fixed values, for a test
of code that builds its own Config from providers, such as an app's
function that loads its configuration:

```go
c := config.New(config.WithProvider(configtest.NewStaticProvider(map[string]any{
	"database": map[string]any{"host": "db"},
})))
if err := c.Load(); err != nil {
	return err
}
```

Each load returns the values, and the Config treats them as any
provider's: their keys are trimmed and lowercased, two keys that become
the same fail the load, and a later provider overrides them. Errors name
the provider `static`. Nil values load nothing.

The provider keeps the map it is given rather than copying it, so do not
change the map while a load may run.


## API reference

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

### type Call

```go
type Call struct {
	Method string
	Args   []any
}
```

Call is one call of a MockConfig method.

<a id="MockConfig"></a>

### type MockConfig

```go
type MockConfig struct {
	SetDefaultFn          func(key string, value any) error
	SetDefaultsFn         func(values any) error
	SetFn                 func(key string, value any) error
	BindFn                func(dest any) error
	GetFn                 func(key string) any
	FindFn                func(key string) (any, bool)
	ValuesFn              func() map[string]any
	LoadProviderFn        func(p config.Provider) error
	LoadProviderContextFn func(ctx context.Context, p config.Provider) error
	Calls                 []Call
	// contains filtered or unexported fields
}
```

MockConfig is a config.ConfigProvider that records its calls, as
described in the package documentation. Each method calls the field
named after it, when it is set.

<a id="NewMockConfigWithValues"></a>

#### func NewMockConfigWithValues

```go
func NewMockConfigWithValues(values map[string]any) (*MockConfig, error)
```

NewMockConfigWithValues returns a MockConfig whose fields are the methods
of a config.Config holding values, as described in the package
documentation.

<a id="MockConfig.Bind"></a>

#### func (*MockConfig) Bind

```go
func (m *MockConfig) Bind(dest any) error
```

Bind records the call and delegates to BindFn if set.

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

#### func (*MockConfig) CallCount

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

CallCount returns the number of times the given method was called.

<a id="MockConfig.Find"></a>

#### func (*MockConfig) Find

```go
func (m *MockConfig) Find(key string) (value any, exist bool)
```

Find records the call and delegates to FindFn if set.

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

#### func (*MockConfig) Get

```go
func (m *MockConfig) Get(key string) any
```

Get records the call and delegates to GetFn if set.

<a id="MockConfig.LoadProvider"></a>

#### func (*MockConfig) LoadProvider

```go
func (m *MockConfig) LoadProvider(p config.Provider) error
```

LoadProvider records the call and delegates to LoadProviderFn if set.

<a id="MockConfig.LoadProviderContext"></a>

#### func (*MockConfig) LoadProviderContext

```go
func (m *MockConfig) LoadProviderContext(ctx context.Context, p config.Provider) error
```

LoadProviderContext records the call, without ctx, and delegates to
LoadProviderContextFn if set.

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

#### func (*MockConfig) Reset

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

Reset clears all recorded calls.

<a id="MockConfig.Set"></a>

#### func (*MockConfig) Set

```go
func (m *MockConfig) Set(key string, value any) error
```

Set records the call and delegates to SetFn if set.

<a id="MockConfig.SetDefault"></a>

#### func (*MockConfig) SetDefault

```go
func (m *MockConfig) SetDefault(key string, value any) error
```

SetDefault records the call and delegates to SetDefaultFn if set.

<a id="MockConfig.SetDefaults"></a>

#### func (*MockConfig) SetDefaults

```go
func (m *MockConfig) SetDefaults(values any) error
```

SetDefaults records the call and delegates to SetDefaultsFn if set.

<a id="MockConfig.Values"></a>

#### func (*MockConfig) Values

```go
func (m *MockConfig) Values() map[string]any
```

Values records the call and delegates to ValuesFn if set.

<a id="StaticProvider"></a>

### type StaticProvider

```go
type StaticProvider struct {
	// contains filtered or unexported fields
}
```

StaticProvider is a config.Provider that loads fixed values, as
described in the package documentation.

<a id="NewStaticProvider"></a>

#### func NewStaticProvider

```go
func NewStaticProvider(values map[string]any) *StaticProvider
```

NewStaticProvider returns a StaticProvider that loads values. It keeps
the map rather than copying it.

<a id="StaticProvider.Load"></a>

#### func (*StaticProvider) Load

```go
func (p *StaticProvider) Load() (map[string]any, error)
```

Load returns the values the provider was given.

<a id="StaticProvider.Name"></a>

#### func (*StaticProvider) Name

```go
func (p *StaticProvider) Name() string
```

Name returns "static".
