Skip to content

Commit b2e6248

Browse files
authored
fix: issue with expectations (#19)
1 parent c89bad0 commit b2e6248

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

http/server.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -216,11 +216,10 @@ func (s *Server) AssertExpectations() {
216216
call += exp.qry.Encode()
217217
}
218218

219-
switch exp.called {
220-
case -1:
219+
switch {
220+
case exp.called == -1:
221221
s.t.Errorf("Expected a call to %s but got none", call)
222-
case 0:
223-
default:
222+
case exp.called > 0:
224223
s.t.Errorf("Expected a call to %s %d times but got called %d times", call, exp.times, exp.times-exp.called)
225224
}
226225
}

http/server_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ func TestServer_HandlesExpectation(t *testing.T) {
2020
res, err := http.Get(s.URL() + "/test/path")
2121
require.NoError(t, err)
2222
assert.Equal(t, 200, res.StatusCode)
23+
24+
s.AssertExpectations()
2325
}
2426

2527
func TestServer_HandlesExpectationWithQuery(t *testing.T) {
@@ -31,6 +33,7 @@ func TestServer_HandlesExpectationWithQuery(t *testing.T) {
3133
res, err := http.Get(s.URL() + "/test/path?p=some%2Fpath")
3234
require.NoError(t, err)
3335
assert.Equal(t, 200, res.StatusCode)
36+
s.AssertExpectations()
3437
}
3538

3639
func TestServer_HandlesAnythingMethodExpectation(t *testing.T) {
@@ -42,6 +45,7 @@ func TestServer_HandlesAnythingMethodExpectation(t *testing.T) {
4245
res, err := http.Post(s.URL()+"/test/path", "text/plain", bytes.NewReader([]byte{}))
4346
require.NoError(t, err)
4447
assert.Equal(t, 200, res.StatusCode)
48+
s.AssertExpectations()
4549
}
4650

4751
func TestServer_HandlesAnythingPathExpectation(t *testing.T) {
@@ -53,6 +57,7 @@ func TestServer_HandlesAnythingPathExpectation(t *testing.T) {
5357
res, err := http.Get(s.URL() + "/test/path")
5458
require.NoError(t, err)
5559
assert.Equal(t, 200, res.StatusCode)
60+
s.AssertExpectations()
5661
}
5762

5863
func TestServer_HandlesWildcardPathExpectation(t *testing.T) {
@@ -64,6 +69,7 @@ func TestServer_HandlesWildcardPathExpectation(t *testing.T) {
6469
res, err := http.Get(s.URL() + "/test/path")
6570
require.NoError(t, err)
6671
assert.Equal(t, 200, res.StatusCode)
72+
s.AssertExpectations()
6773
}
6874

6975
func TestServer_HandlesUnexpectedMethodRequest(t *testing.T) {
@@ -130,6 +136,8 @@ func TestServer_HandlesExpectationNTimes(t *testing.T) {
130136
_, _ = http.Get(s.URL() + "/test/path")
131137
_, _ = http.Get(s.URL() + "/test/path")
132138
_, _ = http.Get(s.URL() + "/test/path")
139+
140+
s.AssertExpectations()
133141
}
134142

135143
func TestServer_HandlesExpectationUnlimitedTimes(t *testing.T) {
@@ -146,6 +154,8 @@ func TestServer_HandlesExpectationUnlimitedTimes(t *testing.T) {
146154

147155
_, _ = http.Get(s.URL() + "/test/path")
148156
_, _ = http.Get(s.URL() + "/test/path")
157+
158+
s.AssertExpectations()
149159
}
150160

151161
func TestServer_ExpectationReturnsBodyBytes(t *testing.T) {

0 commit comments

Comments
 (0)