-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmock_test.go
75 lines (61 loc) · 1.55 KB
/
mock_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
package gohtmock
import (
"io/ioutil"
"net/http"
"testing"
"github.com/stretchr/testify/assert"
)
func TestMock(t *testing.T) {
a := 0
mock := New()
mock.Mock("/test", "ok")
mock.Mock("/callback", "ok", func(*http.Request) int {
a++
return 201
})
resp, err := http.Get(mock.URL() + "/callback")
assert.NoError(t, err)
assert.Equal(t, 201, resp.StatusCode)
resp, err = http.Get(mock.URL() + "/test")
assert.NoError(t, err)
body, err := ioutil.ReadAll(resp.Body)
assert.NoError(t, err)
assert.Equal(t, "ok", string(body))
mock.AssertCallCount(t, "GET", "/test", 1)
mock.AssertCallCount(t, "GET", "/callback", 1)
mock.AssertCallCountAsserted(t)
mock.AssertNoMissingMocks(t)
mock.AssertMocksCalled(t)
assert.Equal(t, 1, a)
}
func TestNotAssertCallCount(t *testing.T) {
mock := New()
mock.Mock("/test", "ok")
newT := &testing.T{}
mock.AssertCallCount(newT, "GET", "/test", 1)
assert.True(t, newT.Failed())
}
func TestNotAssertCallCountAsserted(t *testing.T) {
mock := New()
mock.Mock("/test", "ok")
_, err := http.Get(mock.URL() + "/test")
assert.NoError(t, err)
newT := &testing.T{}
mock.AssertCallCountAsserted(newT)
assert.True(t, newT.Failed())
}
func TestNotAssertMocksCalled(t *testing.T) {
mock := New()
mock.Mock("/test", "ok")
newT := &testing.T{}
mock.AssertMocksCalled(newT)
assert.True(t, newT.Failed())
}
func TestNotAssertNoMissingMocks(t *testing.T) {
mock := New()
_, err := http.Get(mock.URL() + "/test")
assert.NoError(t, err)
newT := &testing.T{}
mock.AssertNoMissingMocks(newT)
assert.True(t, newT.Failed())
}