/
stub_test.go
112 lines (94 loc) · 2.69 KB
/
stub_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
100
101
102
103
104
105
106
107
108
109
110
111
112
package gofake
import (
"errors"
"testing"
"github.com/karlseguin/gspec"
)
func TestStubReturnsTheValueMultipleTimes(t *testing.T) {
spec := gspec.New(t)
fake := newFake()
fake.Stub(fake.GetEmail).Returning("invalid")
spec.Expect(fake.GetEmail("leto")).ToEqual("invalid")
spec.Expect(fake.GetEmail("paul")).ToEqual("invalid")
}
func TestStubReturnsTheDefaultValueMultipleTimes(t *testing.T) {
spec := gspec.New(t)
fake := newFake()
spec.Expect(fake.GetEmail("leto")).ToEqual("leto@caladan.gov")
spec.Expect(fake.GetEmail("paul")).ToEqual("leto@caladan.gov")
}
func TestStubIsLimitedToASingleInvocation(t *testing.T) {
spec := gspec.New(t)
fake := newFake()
fake.Stub(fake.GetEmail).Returning("first").Once()
spec.Expect(fake.GetEmail("leto")).ToEqual("first")
spec.Expect(fake.GetEmail("paul")).ToEqual("leto@caladan.gov")
}
func TestStubIsLimitedToTheSpecifiedNumberOfInvocations(t *testing.T) {
spec := gspec.New(t)
fake := newFake()
fake.Stub(fake.GetEmail).Returning("first").Times(2)
spec.Expect(fake.GetEmail("leto")).ToEqual("first")
spec.Expect(fake.GetEmail("jessica")).ToEqual("first")
spec.Expect(fake.GetEmail("paul")).ToEqual("leto@caladan.gov")
}
//silly, but let's make sure it doesn't panic or anything
func TestStubMethodWithNoReturnIsANoop(t *testing.T) {
fake := newFake()
fake.Exec()
}
func TestStubReturnsAllSpecifiedValues(t *testing.T) {
spec := gspec.New(t)
fake := newFake()
fake.Stub(fake.Count).Returning(32, nil)
c, err := fake.Count()
spec.Expect(c).ToEqual(32)
spec.Expect(err).ToBeNil()
}
func TestStubReturnsASingleSpecifiedValue(t *testing.T) {
spec := gspec.New(t)
fake := newFake()
fake.Stub(fake.Count).Returning(22)
c, err := fake.Count()
spec.Expect(c).ToEqual(22)
spec.Expect(err.Error()).ToEqual("invalid")
}
func TestStubReturnsADefaultOnNil(t *testing.T) {
spec := gspec.New(t)
fake := newFake()
fake.Stub(fake.Count).Returning(nil, "some error")
c, err := fake.Count()
spec.Expect(c).ToEqual(10)
spec.Expect(err.Error()).ToEqual("some error")
}
type Repository interface {
Remove(id int, soft bool) bool
GetEmail(id string) string
Exec()
Count() (int, error)
LogError(err error)
}
type FakeRepository struct {
Fake
}
func newFake() *FakeRepository {
return &FakeRepository{New()}
}
func (f *FakeRepository) GetEmail(id string) string {
r := f.Called(id)
return r.String(0, "leto@caladan.gov")
}
func (f *FakeRepository) Exec() {
f.Called()
}
func (f *FakeRepository) Count() (int, error) {
r := f.Called()
return r.Int(0, 10), r.Error(1, errors.New("invalid"))
}
func (f *FakeRepository) Remove(id int, soft bool) bool {
r := f.Called(id, soft)
return r.Bool(0, true)
}
func (f *FakeRepository) LogError(err error) {
f.Called(err)
}