-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
📝 Document application & calculator packages
- Loading branch information
1 parent
b1c3b97
commit 94b8191
Showing
7 changed files
with
53 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# Go Mock | ||
|
||
An example go library that uses [`go.uber.org/mock`](https://pkg.go.dev/go.uber.org/mock) to easily | ||
mock an external service for unit testing. | ||
|
||
An important `Application` method `CoolAlgorithm` needs to be unit tested. | ||
|
||
`CoolAlgorithm` relies on an external `calculator` service and for whatever reason, that external | ||
service cannot be used during unit testing. | ||
|
||
To remedy this, we first define the `Calculatorer` interface: | ||
|
||
In order to mock this interface, we first have to install `mockgen`: | ||
|
||
```sh | ||
go install go.uber.org/mock/mockgen@latest | ||
``` | ||
|
||
We then use `mockgen` to generate a mock client to use in our unit test: | ||
|
||
```sh | ||
mockgen -source=./calculator/Calculatorer.go -destination=./mock_calculator/Client.go | ||
``` | ||
|
||
Finally, we construct an `Application` instance that uses our mocked client. We set expectations and | ||
return values for the methods that will be called during the `CoolAlgorithm` unit test: | ||
|
||
_TODO: add code examples_ |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package application | ||
|
||
import "pjm.dev/mock/calculator" | ||
|
||
// Application holds the main application state and functionality. | ||
type Application struct { | ||
// Calculator is the application's interface with the calculator service. | ||
Calculator calculator.Calculatorer | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,13 @@ | ||
package calculator | ||
|
||
// Interface that the external calculator service implements. | ||
type Calculatorer interface { | ||
// Add returns the sum of two ints. | ||
Add(int, int) int | ||
// Subtract returns the difference of two ints. | ||
Subtract(int, int) int | ||
// Multiply returns the product of two ints. | ||
Multiply(int, int) int | ||
// Divide returns the quotient of two ints. | ||
Divide(int, int) int | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters