|
1 | 1 | # mock |
2 | | -A modern and type-safe mocking library for Go with full support for generics. |
| 2 | + |
| 3 | +[English](README.md) | [中文](README_CN.md) |
| 4 | + |
| 5 | +**mock** is a modern, type-safe mocking library for the Go programming language, fully supporting generic programming. |
| 6 | +It provides a simple and easy-to-use interface that helps developers easily create and manage mock objects, thereby |
| 7 | +improving the quality and efficiency of unit testing. The library is designed to address the lack of type safety and the |
| 8 | +complexity of traditional mocking tools in Go. |
| 9 | + |
| 10 | +## Key Features |
| 11 | + |
| 12 | +* **Type Safety**: Utilizes Go 1.18+ generics to ensure compile-time safety and avoid runtime type errors |
| 13 | +* **Multiple Mocking Modes**: |
| 14 | + * `Handle` Mode: Directly handle function calls |
| 15 | + * `When/Return` Mode: Conditional mock returns |
| 16 | +* **Flexible Method Matching**: Supports different numbers and types of parameters and return values (up to 5 parameters |
| 17 | + and 5 return values) |
| 18 | +* **Context Support**: Provides integration with the `context` package, making it easier to test in distributed systems |
| 19 | +* **Auto Reset Functionality**: The `Manager` provides a `Reset` method to easily reset all mockers to their initial |
| 20 | + state |
| 21 | +* **Detailed Error Messages**: Offers clear error prompts when no matching mock code is found or when multiple matches |
| 22 | + exist |
| 23 | + |
| 24 | +## Usage Example |
| 25 | + |
| 26 | +Below is a simple usage example: |
| 27 | + |
| 28 | +```go |
| 29 | +package mock_test |
| 30 | + |
| 31 | +import ( |
| 32 | +"context" |
| 33 | +"reflect" |
| 34 | +"testing" |
| 35 | + |
| 36 | +"github.com/go-spring/mock" |
| 37 | +"github.com/go-spring/mock/internal/assert" |
| 38 | +) |
| 39 | + |
| 40 | +type Trace struct { |
| 41 | +TraceId string |
| 42 | +} |
| 43 | + |
| 44 | +type Request struct { |
| 45 | +Token string |
| 46 | +} |
| 47 | + |
| 48 | +type Response struct { |
| 49 | +Message string |
| 50 | +} |
| 51 | + |
| 52 | +type Client struct{} |
| 53 | + |
| 54 | +var clientType = reflect.TypeFor[Client]() |
| 55 | + |
| 56 | +func (c *Client) Get(ctx context.Context, req *Request, trace *Trace) (*Response, error) { |
| 57 | +if ret, ok := mock.InvokeContext(ctx, clientType, "Get", ctx, req, trace); ok { |
| 58 | +return mock.Unbox2[*Response, error](ret) |
| 59 | +} |
| 60 | +return &Response{Message: "9:xxx"}, nil |
| 61 | +} |
| 62 | + |
| 63 | +// MockGet registers a mock implementation for the Get method. |
| 64 | +func MockGet(r *mock.Manager) *mock.Mocker32[context.Context, *Request, *Trace, *Response, error] { |
| 65 | +return mock.NewMocker32[context.Context, *Request, *Trace, *Response, error](r, clientType, "Get") |
| 66 | +} |
| 67 | + |
| 68 | +func TestMockWithContext(t *testing.T) { |
| 69 | +var c Client |
| 70 | + |
| 71 | +// Test case: Unmocked |
| 72 | +{ |
| 73 | +resp, err := c.Get(t.Context(), &Request{}, &Trace{}) |
| 74 | +assert.Nil(t, err) |
| 75 | +assert.Equal(t, resp.Message, "9:xxx") |
| 76 | +} |
| 77 | + |
| 78 | +r := mock.NewManager() |
| 79 | +ctx := r.BindTo(t.Context()) |
| 80 | + |
| 81 | +// Test case: When && Return |
| 82 | +{ |
| 83 | +r.Reset() |
| 84 | +MockGet(r). |
| 85 | +When(func(ctx context.Context, req *Request, trace *Trace) bool { |
| 86 | +return req.Token == "1:abc" |
| 87 | +}). |
| 88 | +Return(func() (resp *Response, err error) { |
| 89 | +return &Response{Message: "1:abc"}, nil |
| 90 | +}) |
| 91 | + |
| 92 | +resp, err := c.Get(ctx, &Request{Token: "1:abc"}, &Trace{}) |
| 93 | +assert.Nil(t, err) |
| 94 | +assert.Equal(t, resp.Message, "1:abc") |
| 95 | +} |
| 96 | + |
| 97 | +// Test case: Handle |
| 98 | +{ |
| 99 | +r.Reset() |
| 100 | +MockGet(r). |
| 101 | +Handle(func(ctx context.Context, req *Request, trace *Trace) (resp *Response, err error) { |
| 102 | +return &Response{Message: "4:xyz"}, nil |
| 103 | +}) |
| 104 | + |
| 105 | +resp, err := c.Get(ctx, &Request{Token: "4:xyz"}, &Trace{}) |
| 106 | +assert.Nil(t, err) |
| 107 | +assert.Equal(t, resp.Message, "4:xyz") |
| 108 | +} |
| 109 | + |
| 110 | +// Test case: Invalid Handle |
| 111 | +{ |
| 112 | +r.Reset() |
| 113 | +MockGet(r).Handle(nil) |
| 114 | + |
| 115 | +resp, err := c.Get(ctx, &Request{}, &Trace{}) |
| 116 | +assert.Nil(t, err) |
| 117 | +assert.Equal(t, resp.Message, "9:xxx") |
| 118 | +} |
| 119 | +} |
| 120 | + |
| 121 | +type ClientInterface interface { |
| 122 | +Query(req *Request, trace *Trace) (*Response, error) |
| 123 | +} |
| 124 | + |
| 125 | +// MockClient is a mock implementation of ClientInterface. |
| 126 | +type MockClient struct { |
| 127 | +r *mock.Manager |
| 128 | +} |
| 129 | + |
| 130 | +var mockClientType = reflect.TypeFor[MockClient]() |
| 131 | + |
| 132 | +// NewMockClient creates a new instance of MockClient. |
| 133 | +func NewMockClient(r *mock.Manager) *MockClient { |
| 134 | +return &MockClient{r} |
| 135 | +} |
| 136 | + |
| 137 | +// Query mocks the Query method by invoking a registered mock implementation. |
| 138 | +func (c *MockClient) Query(req *Request, trace *Trace) (*Response, error) { |
| 139 | +if ret, ok := mock.Invoke(c.r, mockClientType, "Query", req, trace); ok { |
| 140 | +return mock.Unbox2[*Response, error](ret) |
| 141 | +} |
| 142 | +panic("mock error") |
| 143 | +} |
| 144 | + |
| 145 | +// MockQuery registers a mock implementation for the Query method. |
| 146 | +func (c *MockClient) MockQuery() *mock.Mocker22[*Request, *Trace, *Response, error] { |
| 147 | +return mock.NewMocker22[*Request, *Trace, *Response, error](c.r, mockClientType, "Query") |
| 148 | +} |
| 149 | + |
| 150 | +func TestMockNoContext(t *testing.T) { |
| 151 | +r := mock.NewManager() |
| 152 | + |
| 153 | +var c ClientInterface |
| 154 | +mc := NewMockClient(r) |
| 155 | +c = mc |
| 156 | + |
| 157 | +// Test case: When && Return |
| 158 | +{ |
| 159 | +r.Reset() |
| 160 | +mc.MockQuery(). |
| 161 | +When(func(req *Request, trace *Trace) bool { |
| 162 | +return req.Token == "1:abc" |
| 163 | +}). |
| 164 | +Return(func() (resp *Response, err error) { |
| 165 | +return &Response{Message: "1:abc"}, nil |
| 166 | +}) |
| 167 | + |
| 168 | +resp, err := c.Query(&Request{Token: "1:abc"}, &Trace{}) |
| 169 | +assert.Nil(t, err) |
| 170 | +assert.Equal(t, resp.Message, "1:abc") |
| 171 | +} |
| 172 | + |
| 173 | +// Test case: Handle |
| 174 | +{ |
| 175 | +r.Reset() |
| 176 | +mc.MockQuery(). |
| 177 | +Handle(func(req *Request, trace *Trace) (resp *Response, err error) { |
| 178 | +return &Response{Message: "4:xyz"}, nil |
| 179 | +}) |
| 180 | + |
| 181 | +resp, err := c.Query(&Request{Token: "4:xyz"}, &Trace{}) |
| 182 | +assert.Nil(t, err) |
| 183 | +assert.Equal(t, resp.Message, "4:xyz") |
| 184 | +} |
| 185 | + |
| 186 | +// Test case: Invalid Handle |
| 187 | +{ |
| 188 | +r.Reset() |
| 189 | +mc.MockQuery().Handle(nil) |
| 190 | + |
| 191 | +assert.Panic(t, func() { |
| 192 | +_, _ = c.Query(&Request{}, &Trace{}) |
| 193 | +}, "mock error") |
| 194 | +} |
| 195 | +} |
| 196 | +``` |
| 197 | + |
| 198 | +## License |
| 199 | + |
| 200 | +This project is licensed under the Apache License Version 2.0. |
0 commit comments