https://github.com/somatech1/mocks
A package to help creating mocks for APIs to be used in unit tests
https://github.com/somatech1/mocks
mock testing unit
Last synced: 4 months ago
JSON representation
A package to help creating mocks for APIs to be used in unit tests
- Host: GitHub
- URL: https://github.com/somatech1/mocks
- Owner: somatech1
- License: mpl-2.0
- Created: 2024-02-01T23:56:20.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2024-04-02T19:55:35.000Z (about 2 years ago)
- Last Synced: 2025-12-17T16:52:12.458Z (6 months ago)
- Topics: mock, testing, unit
- Language: Go
- Homepage:
- Size: 25.4 KB
- Stars: 2
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# mock_s
## About
This package provides an easier way to mock services for testing purposes.
The wrapper on the [mockgen](https://github.com/uber-go/mock) gives you a
nicer syntax to write mocks.
## Usage example
```go
package main
import (
"context"
"testing"
"github.com/stretchr/testify/assert"
"github.com/somatech1/mocks"
)
func TestFoo(t *testing.T) {
ctx := context.TODO()
a := assert.New(t)
// You can explicitly define the mock type
// NewMock[example_mock.MockExampleMockMockRecorder]
// or let the compiler infer it
mock := mocks.New(
t,
example_mock.NewMockExampleMock,
)
expectedInput := "Hello World"
expectedOutput := "Mocked Output"
mock.Mock(&mocks.MockOptions{
Ctx: ctx,
Call: mock.Recorder().GetByString,
Times: 1,
Input: expectedInput,
Return: expectedOutput,
})
c := mock.Client()
output, err := c.GetByString(ctx, expectedInput)
a.NoError(err)
a.Equal(output, expectedOutput)
}
```
See more [examples](service_mock_test.go)
## License
[Mozilla Public License 2.0](LICENSE)