File tree 7 files changed +53
-14
lines changed
7 files changed +53
-14
lines changed Original file line number Diff line number Diff line change
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_
Load Diff This file was deleted.
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change 1
- package app
1
+ package application
2
2
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
5
6
6
7
output = application .Calculator .Add (output , 0 )
7
8
output = application .Calculator .Subtract (output , 0 )
Original file line number Diff line number Diff line change 1
- package app_test
1
+ package application_test
2
2
3
3
import (
4
4
"testing"
5
5
6
6
"go.uber.org/mock/gomock"
7
- "pjm.dev/mock/app "
7
+ "pjm.dev/mock/application "
8
8
"pjm.dev/mock/mock_calculator"
9
9
)
10
10
11
11
func TestCoolAlgorithm (t * testing.T ) {
12
12
ctrl := gomock .NewController (t )
13
13
mockCalculator := mock_calculator .NewMockCalculatorer (ctrl )
14
-
14
+ application := application. Application { Calculator : mockCalculator }
15
15
number := 100
16
16
17
17
mockCalculator .
@@ -38,7 +38,6 @@ func TestCoolAlgorithm(t *testing.T) {
38
38
Times (1 ).
39
39
Return (number )
40
40
41
- application := app.Application {Calculator : mockCalculator }
42
41
if application .CoolAlgorithm (number ) != number {
43
42
t .Fail ()
44
43
}
Original file line number Diff line number Diff line change 1
1
package calculator
2
2
3
+ // Interface that the external calculator service implements.
3
4
type Calculatorer interface {
5
+ // Add returns the sum of two ints.
4
6
Add (int , int ) int
7
+ // Subtract returns the difference of two ints.
5
8
Subtract (int , int ) int
9
+ // Multiply returns the product of two ints.
6
10
Multiply (int , int ) int
11
+ // Divide returns the quotient of two ints.
7
12
Divide (int , int ) int
8
13
}
Original file line number Diff line number Diff line change 1
1
package calculator
2
2
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.
3
7
type Client struct {}
4
8
5
9
func (client * Client ) Add (a int32 , b int32 ) int32 {
You can’t perform that action at this time.
0 commit comments