-
-
Notifications
You must be signed in to change notification settings - Fork 457
/
Copy pathgomock_example.go
44 lines (35 loc) · 839 Bytes
/
gomock_example.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package main
import "fmt"
// The non-mock implementation of the client
type MyApiClient struct{}
type Server struct {
Client ApiClient
}
// We can imagine that this function makes some network call during its execution.
func (a *MyApiClient) ApiDouble(valueToDouble int) (int, error) {
return valueToDouble * 2, nil
}
// Generates a new MyApiClient
func NewMyApiClient() *MyApiClient {
return &MyApiClient{}
}
// Generates a new server
func NewServer(client ApiClient) *Server {
return &Server{
Client: client,
}
}
func (s *Server) FunctionToTest(val int) (int, error) {
// this is our imaginary network call
res, err := s.Client.ApiDouble(val)
if err != nil {
return 0, err
}
return res, nil
}
func main() {
cli := NewMyApiClient()
server := NewServer(cli)
res, _ := server.FunctionToTest(2)
fmt.Println(res)
}