Skip to content

Commit 94b8191

Browse files
📝 Document application & calculator packages
1 parent b1c3b97 commit 94b8191

File tree

7 files changed

+53
-14
lines changed

7 files changed

+53
-14
lines changed

README.md

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Go Mock
2+
3+
An example go library that uses [`go.uber.org/mock`](https://pkg.go.dev/go.uber.org/mock) to easily
4+
mock an external service for unit testing.
5+
6+
An important `Application` method `CoolAlgorithm` needs to be unit tested.
7+
8+
`CoolAlgorithm` relies on an external `calculator` service and for whatever reason, that external
9+
service cannot be used during unit testing.
10+
11+
To remedy this, we first define the `Calculatorer` interface:
12+
13+
In order to mock this interface, we first have to install `mockgen`:
14+
15+
```sh
16+
go install go.uber.org/mock/mockgen@latest
17+
```
18+
19+
We then use `mockgen` to generate a mock client to use in our unit test:
20+
21+
```sh
22+
mockgen -source=./calculator/Calculatorer.go -destination=./mock_calculator/Client.go
23+
```
24+
25+
Finally, we construct an `Application` instance that uses our mocked client. We set expectations and
26+
return values for the methods that will be called during the `CoolAlgorithm` unit test:
27+
28+
_TODO: add code examples_

app/Application.go

-7
This file was deleted.

application/Application.go

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package application
2+
3+
import "pjm.dev/mock/calculator"
4+
5+
// Application holds the main application state and functionality.
6+
type Application struct {
7+
// Calculator is the application's interface with the calculator service.
8+
Calculator calculator.Calculatorer
9+
}

app/CoolAlgorithm.go renamed to application/CoolAlgorithm.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
package app
1+
package application
22

3-
func (application *Application) CoolAlgorithm(x int) int {
4-
var output int = x
3+
// CoolAlgorithm is an important bit of Application functionality that relies on the Calculatorer interface.
4+
func (application *Application) CoolAlgorithm(input int) int {
5+
var output int = input
56

67
output = application.Calculator.Add(output, 0)
78
output = application.Calculator.Subtract(output, 0)

app/CoolAlgorithm_test.go renamed to application/CoolAlgorithm_test.go

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
1-
package app_test
1+
package application_test
22

33
import (
44
"testing"
55

66
"go.uber.org/mock/gomock"
7-
"pjm.dev/mock/app"
7+
"pjm.dev/mock/application"
88
"pjm.dev/mock/mock_calculator"
99
)
1010

1111
func TestCoolAlgorithm(t *testing.T) {
1212
ctrl := gomock.NewController(t)
1313
mockCalculator := mock_calculator.NewMockCalculatorer(ctrl)
14-
14+
application := application.Application{Calculator: mockCalculator}
1515
number := 100
1616

1717
mockCalculator.
@@ -38,7 +38,6 @@ func TestCoolAlgorithm(t *testing.T) {
3838
Times(1).
3939
Return(number)
4040

41-
application := app.Application{Calculator: mockCalculator}
4241
if application.CoolAlgorithm(number) != number {
4342
t.Fail()
4443
}

calculator/Calculatorer.go

+5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
package calculator
22

3+
// Interface that the external calculator service implements.
34
type Calculatorer interface {
5+
// Add returns the sum of two ints.
46
Add(int, int) int
7+
// Subtract returns the difference of two ints.
58
Subtract(int, int) int
9+
// Multiply returns the product of two ints.
610
Multiply(int, int) int
11+
// Divide returns the quotient of two ints.
712
Divide(int, int) int
813
}

calculator/Client.go

+4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
package calculator
22

3+
// Client is a toy implementation of the Calculatorer interface that can't be used in unit tests.
4+
//
5+
// In the real application, this would use whatever protocol the calculator service uses to
6+
// communicate.
37
type Client struct{}
48

59
func (client *Client) Add(a int32, b int32) int32 {

0 commit comments

Comments
 (0)