-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbus_test.go
73 lines (68 loc) · 1.75 KB
/
bus_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
package bus_test
import (
"context"
"errors"
"testing"
"github.com/theskyinflames/cqrs-eda/pkg/bus"
"github.com/stretchr/testify/require"
)
func TestBusDispatch(t *testing.T) {
var (
randomErr = errors.New("")
response = "a response"
)
tests := []struct {
name string
handlerName string
handler bus.Handler
dispatchable bus.Dispatchable
expected interface{}
expectedErrFunc func(t *testing.T, err error)
}{
{
name: `Given an unknown handler, when it's called, then an error is returned`,
handlerName: "unknown",
handler: handlerFixture(nil, nil),
dispatchable: &DispatchableMock{},
expectedErrFunc: func(t *testing.T, err error) {
require.ErrorIs(t, err, bus.ErrNotDispatchable)
},
},
{
name: `Given a dispatchable that makes its handler returns an error,
when it's called, then an error is returned`,
handlerName: "h",
handler: handlerFixture(nil, randomErr),
dispatchable: &DispatchableMock{
NameFunc: func() string {
return "h"
},
},
expectedErrFunc: func(t *testing.T, err error) {
require.ErrorIs(t, err, randomErr)
},
},
{
name: `Given a dispatchable, when it's called, then an error is returned`,
handlerName: "h",
handler: handlerFixture(response, nil),
dispatchable: &DispatchableMock{
NameFunc: func() string {
return "h"
},
},
expected: response,
},
}
for _, tt := range tests {
b := bus.New()
b.Register(tt.handlerName, tt.handler)
response, err := b.Dispatch(context.Background(), tt.dispatchable)
require.Equal(t, tt.expectedErrFunc == nil, err == nil)
if err != nil {
tt.expectedErrFunc(t, err)
continue
}
require.Equal(t, tt.expected, response)
}
}