-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmocker_amd64_test.go
63 lines (54 loc) · 1.49 KB
/
mocker_amd64_test.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// Package mocker_test 对 mocker 包的测试
// 当前文件实现了对 mocker.go 的单测
package mocker_test
import (
"errors"
"fmt"
"testing"
"time"
"github.com/stretchr/testify/suite"
mocker "github.com/tencent/goom"
"github.com/tencent/goom/test"
)
// TestUnitAmd64TestSuite 测试入口
func TestUnitAmd64TestSuite(t *testing.T) {
// 开启 debug
// 1.可以查看 apply 和 reset 的状态日志
// 2.查看 mock 调用日志
mocker.OpenDebug()
suite.Run(t, new(mockerTestAmd64Suite))
}
type mockerTestAmd64Suite struct {
suite.Suite
fakeErr error
}
func (s *mockerTestAmd64Suite) SetupTest() {
s.fakeErr = errors.New("fake error")
}
// TestCallOrigin 测试调用原函数 mock return
func (s *mockerTestAmd64Suite) TestCallOrigin() {
s.Run("success", func() {
// 定义原函数,用于占位,实际不会执行该函数体
var origin = func(i int) int {
// 用于占位,实际不会执行该函数体, 但是必须编写
fmt.Println("only for placeholder, will not call")
// return 任意值
return 0
}
mock := mocker.Create()
mock.Func(test.Foo).Origin(&origin).Apply(func(i int) int {
originResult := origin(i)
fmt.Printf("arguments are %v\n", i)
return originResult + 100
})
s.Equal(101, test.Foo(1), "foo mock check")
mock.Reset()
s.Equal(1, test.Foo(1), "foo mock reset check")
})
}
func (s *mockerTestAmd64Suite) TestUnitEmptyMatch() {
s.Run("empty return", func() {
mocker.Create().Func(time.Sleep).Return()
time.Sleep(time.Second)
})
}