-
Notifications
You must be signed in to change notification settings - Fork 0
/
call_return_test.go
99 lines (85 loc) · 2.14 KB
/
call_return_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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package funcmock
import (
"errors"
"testing"
. "github.com/onsi/gomega"
)
func TestCall0thReturn(t *testing.T) {
RegisterTestingT(t)
var swap = func(i, j int) (int, int) {
return j, i
}
var swapMock = Mock(&swap)
swapMock.NthCall(0).SetReturn(8, 9)
Expect(swapMock.NthCall(0)).NotTo(BeNil())
v8, v9 := swap(2, 2)
Expect(v8).To(Equal(8))
Expect(v9).To(Equal(9))
}
func TestCallLastReturn(t *testing.T) {
RegisterTestingT(t)
var swap = func(i, j int) (int, int) {
return j, i
}
var swapMock = Mock(&swap)
swapMock.NthCall(2).SetReturn(8, 9)
_, _ = swap(2, 2)
_, _ = swap(2, 2)
v8, v9 := swap(2, 2)
Expect(v8).To(Equal(8))
Expect(v9).To(Equal(9))
}
func TestSetDefaultReturnWrongType(t *testing.T) {
RegisterTestingT(t)
var swap = func(i, j int) (int, int) {
return j, i
}
_, _ = swap(1, 1)
var swapMock = Mock(&swap)
var panicMessage interface{}
func() {
defer func() {
panicMessage = recover()
}()
swapMock.SetDefaultReturn("three", "four")
}()
Expect(panicMessage).To(Equal("reflect.Value.Convert: value of type string cannot be converted to type int"))
}
func TestSetDefaultReturnNil(t *testing.T) {
RegisterTestingT(t)
var swap = func(i, j int) (*int, *int) {
return &j, &i
}
_, _ = swap(1, 1)
var swapMock = Mock(&swap)
swapMock.SetDefaultReturn(nil, nil)
Expect(func() { _, _ = swap(2, 2) }).NotTo(Panic())
v8, v9 := swap(2, 2)
Expect(v8).To(BeNil())
Expect(v9).To(BeNil())
}
func TestSetReturnNil(t *testing.T) {
RegisterTestingT(t)
var swap = func(i, j int) (*int, *int) {
return &j, &i
}
_, _ = swap(1, 1)
var swapMock = Mock(&swap)
swapMock.NthCall(0).SetReturn(nil, nil)
Expect(func() { _, _ = swap(2, 2) }).NotTo(Panic())
v8, v9 := swap(2, 2)
Expect(v8).To(BeNil())
Expect(v9).To(BeNil())
}
func TestSetReturnWithErrorReturnParam(t *testing.T) {
RegisterTestingT(t)
var funcToTest = func(i int, j int) (err error) {
return err
}
var swapMock = Mock(&funcToTest)
swapMock.NthCall(0).SetReturn(errors.New("message"))
Expect(func() { _ = funcToTest(2, 2) }).NotTo(Panic())
swapMock.NthCall(1).SetReturn(errors.New("message"))
err := funcToTest(2, 2)
Expect(err.Error()).To(Equal("message"))
}