Skip to content

Commit a54f22b

Browse files
lvan100lianghuan
authored andcommitted
docs(README): update readme doc
1 parent 6221dd6 commit a54f22b

File tree

2 files changed

+86
-0
lines changed

2 files changed

+86
-0
lines changed

README.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,50 @@ complexity of traditional mocking tools in Go.
2121
* **Detailed Error Messages**: Offers clear error prompts when no matching mock code is found or when multiple matches
2222
exist
2323

24+
## Installation Tool
25+
26+
**gsmock** is a tool used to generate Go mock code. You can install it with the following command:
27+
28+
```bash
29+
go install github.com/go-spring/mock/gsmock@latest
30+
```
31+
32+
### Basic Usage
33+
34+
1. **Define an Interface**
35+
36+
First, define the interface you want to mock in your project. For example, create a file named `service.go` and add the
37+
following code:
38+
39+
```go
40+
package main
41+
42+
type Service interface {
43+
Save(r1, r2, r3, r4, r5, r6 int)
44+
}
45+
```
46+
47+
2. **Generate Mock Code**
48+
49+
Then, add a `go:generate` directive to the `service.go` file to generate the mock code:
50+
51+
```go
52+
//go:generate gsmock
53+
```
54+
55+
You need to specify an output filename, such as `service_mock.go`, otherwise the output will be printed to the console.
56+
57+
```go
58+
//go:generate gsmock -o src_mock.go
59+
```
60+
61+
You can also specify which interfaces to generate mocks for and which to exclude (prefix the interface name with `!` to
62+
exclude it).
63+
64+
```go
65+
//go:generate gsmock -o src_mock.go -i '!RepositoryV2,Repository'
66+
```
67+
2468
## Usage Example
2569

2670
Below is a simple usage example:

README_CN.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,48 @@ mocking 工具存在的类型安全性不足和使用复杂性问题。
1717
- **自动重置功能**:Manager 提供 Reset 方法,可轻松重置所有模拟器到初始状态
1818
- **详细的错误信息**:当没有匹配的 mock 代码或存在多个匹配时,提供清晰的错误提示
1919

20+
## 安装工具
21+
22+
gsmock 是一个用于生成 Go mock 代码的工具,你可以通过以下方式安装它:
23+
24+
```bash
25+
go install github.com/go-spring/mock/gsmock@latest
26+
```
27+
28+
### 基本用法
29+
30+
1. 定义接口
31+
32+
首先,在你的项目中定义需要 mock 的接口。例如,创建一个名为 service.go 的文件,并添加如下代码:
33+
34+
```go
35+
package main
36+
37+
type Service interface {
38+
Save(r1, r2, r3, r4, r5, r6 int)
39+
}
40+
```
41+
42+
2. 生成 Mock 代码
43+
44+
然后在 service.go 文件中加入 go:generate 指令,即可生成 mock 代码:
45+
46+
```go
47+
//go:generate gsmock
48+
```
49+
50+
你需要指定一个输出文件名,例如 service_mock.go,否则会输出到控制台上。
51+
52+
```go
53+
//go:generate gsmock -o src_mock.go
54+
```
55+
56+
你还可以指定哪些接口生成 mock,哪些接口不生成 mock (在接口名前面加!即可)。
57+
58+
```go
59+
//go:generate gsmock -o src_mock.go -i '!RepositoryV2,Repository'
60+
```
61+
2062
## 使用示例
2163

2264
以下是一个简单的使用示例:

0 commit comments

Comments
 (0)