-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c80e447
commit 0447b58
Showing
3 changed files
with
38 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package mock | ||
|
||
import ( | ||
"fmt" | ||
"github.com/agiledragon/gomonkey/v2" | ||
"testing" | ||
) | ||
|
||
func DoSomething() string { | ||
return "Real Result" | ||
} | ||
|
||
// go tool evn 加上这个参数才会生效-gcflags=all=-l | ||
// fixme 程序很容易崩溃,gomonkey有bug | ||
func TestCompute(t *testing.T) { | ||
// 使用 gomonkey 替换 DoSomething 函数的实现 | ||
patch := gomonkey.ApplyFunc(DoSomething, func() string { | ||
return "Mocked Result" | ||
}) | ||
defer patch.Reset() // 确保测试结束后恢复原始函数 | ||
|
||
// 验证函数返回的是我们模拟的结果 | ||
result := DoSomething() | ||
fmt.Println(result) | ||
} |