-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathscoped_test.go
152 lines (111 loc) · 3.68 KB
/
scoped_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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package mocha
import (
"fmt"
"net/http"
"testing"
"github.com/stretchr/testify/assert"
"github.com/vitorsalgado/mocha/v3/expect"
"github.com/vitorsalgado/mocha/v3/internal/testmocks"
"github.com/vitorsalgado/mocha/v3/internal/testutil"
"github.com/vitorsalgado/mocha/v3/reply"
)
func TestScoped(t *testing.T) {
m1 := newMock()
m2 := newMock()
m3 := newMock()
repo := newStorage()
repo.Save(m1)
repo.Save(m2)
repo.Save(m3)
scoped := scope(repo, repo.FetchAll())
assert.Equal(t, 3, len(scoped.ListAll()))
assert.Equal(t, m1, scoped.Get(m1.ID))
t.Run("should not return done when there is still pending mocks", func(t *testing.T) {
fakeT := testmocks.NewFakeNotifier()
assert.False(t, scoped.Called())
assert.Equal(t, 3, len(scoped.ListPending()))
assert.True(t, scoped.IsPending())
scoped.AssertCalled(fakeT)
fakeT.AssertNumberOfCalls(t, "Errorf", 1)
})
t.Run("should return done when all mocks were called", func(t *testing.T) {
fakeT := testmocks.NewFakeNotifier()
m1.Hit()
assert.False(t, scoped.Called())
scoped.AssertCalled(fakeT)
m2.Hit()
m3.Hit()
fakeT.AssertNumberOfCalls(t, "Errorf", 1)
assert.True(t, scoped.AssertCalled(t))
assert.True(t, scoped.Called())
assert.Equal(t, 0, len(scoped.ListPending()))
assert.False(t, scoped.IsPending())
})
t.Run("should return total hits from mocks", func(t *testing.T) {
assert.Equal(t, 3, scoped.Hits())
})
t.Run("should clean all mocks associated with scope when calling .Clean()", func(t *testing.T) {
scoped.Clean()
assert.Equal(t, 0, len(scoped.ListPending()))
assert.False(t, scoped.IsPending())
})
t.Run("should only consider enabled mocks", func(t *testing.T) {
m := New(t)
m.Start()
s1 := m.AddMocks(Get(expect.URLPath("/test1")).Reply(reply.OK()))
s2 := m.AddMocks(
Get(expect.URLPath("/test2")).Reply(reply.OK()),
Get(expect.URLPath("/test3")).Reply(reply.OK()))
t.Run("initial state (enabled)", func(t *testing.T) {
req := testutil.Get(fmt.Sprintf("%s/test1", m.URL()))
res, err := req.Do()
assert.NoError(t, err)
assert.Equal(t, http.StatusOK, res.StatusCode)
req = testutil.Get(fmt.Sprintf("%s/test2", m.URL()))
res, err = req.Do()
assert.NoError(t, err)
assert.Equal(t, http.StatusOK, res.StatusCode)
req = testutil.Get(fmt.Sprintf("%s/test3", m.URL()))
res, err = req.Do()
assert.NoError(t, err)
assert.Equal(t, http.StatusOK, res.StatusCode)
assert.True(t, s1.Called())
assert.True(t, s2.Called())
})
t.Run("disabled", func(t *testing.T) {
s1.Disable()
req := testutil.Get(fmt.Sprintf("%s/test1", m.URL()))
res, err := req.Do()
assert.NoError(t, err)
assert.Equal(t, http.StatusTeapot, res.StatusCode)
req = testutil.Get(fmt.Sprintf("%s/test2", m.URL()))
res, err = req.Do()
assert.NoError(t, err)
assert.Equal(t, http.StatusOK, res.StatusCode)
req = testutil.Get(fmt.Sprintf("%s/test3", m.URL()))
res, err = req.Do()
assert.NoError(t, err)
assert.Equal(t, http.StatusOK, res.StatusCode)
assert.Equal(t, 1, s1.Hits())
})
t.Run("enabling previously disabled", func(t *testing.T) {
s1.Enable()
req := testutil.Get(fmt.Sprintf("%s/test1", m.URL()))
res, err := req.Do()
assert.NoError(t, err)
assert.Equal(t, http.StatusOK, res.StatusCode)
assert.Equal(t, 2, s1.Hits())
})
t.Run("disabling multiple", func(t *testing.T) {
s2.Disable()
req := testutil.Get(fmt.Sprintf("%s/test2", m.URL()))
res, err := req.Do()
assert.NoError(t, err)
assert.Equal(t, http.StatusTeapot, res.StatusCode)
req = testutil.Get(fmt.Sprintf("%s/test3", m.URL()))
res, err = req.Do()
assert.NoError(t, err)
assert.Equal(t, http.StatusTeapot, res.StatusCode)
})
})
}