Add note to readme about how to generate mocks which follow the naming scheme

This commit is contained in:
Jan-Eric Hellenberg
2021-05-10 12:42:44 +02:00
parent c9a5299f0c
commit 9a2181afc9
2 changed files with 35 additions and 7 deletions

View File

@ -51,9 +51,9 @@ test:
script:
- go test $(go list ./... | grep -v /e2e_tests) -v -coverprofile coverage.cov
- # exclude Mock files from coverage report
- cat coverage.cov | grep -v Mock.go > coverage.cov || true
- go tool cover -func=coverage.cov
- go tool cover -html=coverage.cov -o coverage_unit.html
- cat coverage.cov | grep -v Mock.go > coverage_cleaned.cov || true
- go tool cover -func=coverage_cleaned.cov
- go tool cover -html=coverage_cleaned.cov -o coverage_unit.html
artifacts:
paths:
- coverage_unit.html

View File

@ -89,7 +89,35 @@ $ openssl req -x509 -nodes -newkey rsa:2048 -keyout server.rsa.key -out server.r
## Tests
As testing framework we use the [testify](https://github.com/stretchr/testify) toolkit.
For mocks we use [mockery](https://github.com/vektra/mockery).
With Mockery, you can create mocks by running `mockery -r --name=<<interface_name>>` on a specific interface.
If the interface changes, you can rerun this command.
For e2e tests we provide a separate package. E2e tests require the connection to a Nomad cluster.
### Mocks
For mocks we use [mockery](https://github.com/vektra/mockery). To generate a mock, first navigate to the package the interface is defined in.
You can then create a mock for the interface of your choice by running
```bash
mockery \
--name=<<interface_name>> \
--structname=<<interface_name>>Mock \
--filename=<<interface_name>>Mock.go \
--output=<<relative_path_to_output_folder>> \
--outpkg=<<package_name_of_mock>>
```
on a specific interface.
For example, for an interface called `ExecutorApi` in the package `nomad`, you might run
```bash
mockery \
--name=ExecutorApi \
--output='.' \
--structname=ExecutorApiMock \
--filename=ExecutorApiMock.go \
--outpkg=nomad
```
Note that the default value for `--output` is `./mocks` and the default for `--outpkg` is `mocks`. This will create the mock in a `mocks` sub-folder. However, in some cases (if the mock implements private interface methods), it needs to be in the same package as the interface it is mocking.
If the interface changes, you can rerun this command.