Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/ansermino/go-mock-demo
https://github.com/ansermino/go-mock-demo
Last synced: about 1 month ago
JSON representation
- Host: GitHub
- URL: https://github.com/ansermino/go-mock-demo
- Owner: ansermino
- Created: 2021-06-09T15:40:33.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2021-06-09T15:44:08.000Z (over 3 years ago)
- Last Synced: 2024-06-20T14:17:56.548Z (6 months ago)
- Language: Go
- Size: 24.4 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Go Mocking & Interfaces Demo
There are two major libs for mocking:
- [golang/mock](https://github.com/golang/mock) (we'll focus on this one)
- [testify/mock](https://github.com/stretchr/testify)## Setup `mockgen`
```bash
# Go version < 1.16
GO111MODULE=on go get github.com/golang/mock/[email protected]
```
or
```bash
# Go 1.16+
go install github.com/golang/mock/[email protected]
```## Generating Mocks
```bash
mockgen -source basic/basic.go -destination basic/mock/basic.go
mockgen -source ethclient/ethclient.go -destination ethclient/mock/ethclient.go
```## Basic Example
This is a simple example of an interface being used for a database. We can generate mocks for this interface and then write tests using it. See `TestStoreMulti`.
Note that we explicitly specify the expected interactions with the interface, thus we can verify exactly which calls are being made. In `TestStoreMultiFailing` we have specified an expected call which is different from the one that is made given the inputs, thus the test fails.
## Ethclient Example
Here we have a struct which uses `ethclient.Client`.
We have replaced the usage of `ethclient.Client` with an interface - very easy, much wow!
We can generate mocks for this interface and easily test the functionality of the `ChainReader`.
Once you start using more interfaces, you can refactor your code to be much more modular, like we have with `CheckBlocks` which removes the need for the ChainReader struct.
## Notes
### Interfaces
- By convention, one-method interfaces are named by the method name plus an -er suffix or similar modification to construct an agent noun: Reader, Writer, Formatter, CloseNotifier etc. ([source](https://golang.org/doc/effective_go#interface-names))
- Smaller interfaces are better
- Be cautious which types you use as this may restrict the usefulness of an interface