-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmocker_test.go
More file actions
110 lines (89 loc) · 2.86 KB
/
mocker_test.go
File metadata and controls
110 lines (89 loc) · 2.86 KB
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
100
101
102
103
104
105
106
107
108
109
110
package interfacemocker
import (
"errors"
"reflect"
"testing"
"github.com/stretchr/testify/assert"
)
func Test_Mocker_success(t *testing.T) {
mocks := []interface{}{
[]interface{}{
Foo{}, errors.New("foo"),
},
Bar{},
[]interface{}{
Foo{}, nil,
},
// test pointer
&Baz{},
// test multi value return pointer with error
[]interface{}{
&Waldo{}, errors.New("foo"),
},
// test multi value return with nil error
[]interface{}{
&Waldo{}, nil,
},
errors.New("test error"),
nil,
}
mocker := NewMocker(mocks)
r1, err := mocker.GetFoo()
assert.Error(t, err)
assert.Equal(t, reflect.TypeOf(r1).String(), "interfacemocker.Foo")
r2 := mocker.GetBar()
assert.Equal(t, reflect.TypeOf(r2).String(), "interfacemocker.Bar")
r3, err := mocker.GetFoo()
assert.NoError(t, err)
assert.Equal(t, reflect.TypeOf(r3).String(), "interfacemocker.Foo")
r4 := mocker.GetBaz()
assert.Equal(t, reflect.TypeOf(r4).String(), "*interfacemocker.Baz")
r5, err := mocker.GetWaldo()
assert.Error(t, err)
assert.Equal(t, reflect.TypeOf(r5).String(), "*interfacemocker.Waldo")
r6, err := mocker.GetWaldo()
assert.NoError(t, err)
assert.Equal(t, reflect.TypeOf(r6).String(), "*interfacemocker.Waldo")
r7 := mocker.GetError()
assert.Equal(t, reflect.TypeOf(r7).String(), "*errors.errorString")
r8 := mocker.GetError()
assert.Nil(t, r8)
}
// test panic when wrong type is returned, not matching the mocks list
func Test_Mocker_getSingleMock_panics(t *testing.T) {
mocks := []interface{}{Bar{}, &Baz{}}
mocker := NewMocker(mocks)
r1 := mocker.GetBar()
assert.Equal(t, reflect.TypeOf(r1).String(), "interfacemocker.Bar")
expectedPanicMsg := "Mocker expected '*interfacemocker.Baz' return value, but got 'interfacemocker.Bar' on call 1"
assert.PanicsWithValue(t, expectedPanicMsg, func() { mocker.GetBar() })
}
func Test_Mocker_checkIteration_panics(t *testing.T) {
mocks := []interface{}{}
mocker := NewMocker(mocks)
expectPanicMsg := "no more mocks available"
assert.PanicsWithValue(t, expectPanicMsg, func() { mocker.GetFoo() })
}
func Test_Mocker_getMultiMock_not_a_slice_panic(t *testing.T) {
mocks := []interface{}{Bar{}}
mocker := NewMocker(mocks)
assert.PanicsWithValue(t, "mocker expected slice, but got 'interfacemocker.Bar' on call 0", func() { mocker.GetFoo() })
}
func Test_Mocker_getMultiMock_invalid_mock_slice_length_panic(t *testing.T) {
mocks := []interface{}{
[]interface{}{
Foo{},
},
}
mocker := NewMocker(mocks)
assert.PanicsWithValue(t, "mocker expected 2 values, but got 1 on call 0", func() { mocker.GetFoo() })
}
func Test_Mocker_getMultiMock_invalid_mock_return_value_panic(t *testing.T) {
mocks := []interface{}{
[]interface{}{
Foo{}, nil,
},
}
mocker := NewMocker(mocks)
assert.PanicsWithValue(t, "mocker expected 'interfacemocker.Waldo', but got 'interfacemocker.Foo' on call 0 and position 0", func() { mocker.GetWaldo() })
}