-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandler_mock_test.go
77 lines (61 loc) · 1.77 KB
/
handler_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
76
77
package mediator
import (
"context"
"reflect"
"testing"
)
func TestMockHandler(t *testing.T) {
if len(handlers) > 0 {
t.Fatal("invalid test: one or more handlers are already registered")
}
mock, reg := MockHandler[string, string]()
defer reg.Remove()
t.Run("registers the handler", func(t *testing.T) {
wanted := 1
got := len(handlers)
if wanted != got {
t.Errorf("wanted %d, got %d", wanted, got)
}
})
// ACT
_, err := Perform[string, string](context.Background(), "test")
if err != nil {
t.Errorf("unexpected error: %v", err)
}
t.Run("captures number of handled requests", func(t *testing.T) {
wanted := 1
got := mock.NumRequests()
if wanted != got {
t.Errorf("wanted %d, got %d", wanted, got)
}
})
t.Run("returns a copy of handled requests", func(t *testing.T) {
requests := mock.Requests()
if reflect.ValueOf(requests).UnsafePointer() == reflect.ValueOf(mock.requests).UnsafePointer() {
t.Error("got same slice")
}
if !reflect.DeepEqual(requests, mock.requests) {
t.Errorf("wanted %v, got %v", mock.requests, requests)
}
})
t.Run("captures that a handler was called", func(t *testing.T) {
wantedWc := true
wantedWnc := false
gotWc := mock.WasCalled()
gotWnc := mock.WasNotCalled()
if wantedWc != gotWc || wantedWnc != gotWnc {
t.Errorf("called / not called: wanted %v / %v, got %v / %v", wantedWc, wantedWnc, gotWc, gotWnc)
}
})
t.Run("captures that a handler was not called", func(t *testing.T) {
mock, reg := MockHandler[int, int]()
defer reg.Remove()
wantedWc := false
wantedWnc := true
gotWc := mock.WasCalled()
gotWnc := mock.WasNotCalled()
if wantedWc != gotWc || wantedWnc != gotWnc {
t.Errorf("called / not called: wanted %v / %v, got %v / %v", wantedWc, wantedWnc, gotWc, gotWnc)
}
})
}