forked from chiguirez/cromberbus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cromberbus_test.go
101 lines (92 loc) · 3.09 KB
/
cromberbus_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
package cromberbus
import (
"testing"
"github.com/stretchr/testify/require"
)
type ACommandHandler struct {
NumberOfHandleCalls int
}
func (h *ACommandHandler) Handle(command struct{}) error {
h.NumberOfHandleCalls++
return nil
}
type AMiddleware struct {
NumberOfExecuteCalls int
}
func (m *AMiddleware) Execute(command Command, next CommandCallable) error {
m.NumberOfExecuteCalls++
return next(command)
}
func TestMapHandlerResolver_Resolve(t *testing.T) {
isRequire := require.New(t)
command := struct{}{}
t.Run("Given a map handler resolver", func(t *testing.T) {
sut := NewMapHandlerResolver()
t.Run("When command handler is not found", func(t *testing.T) {
handler, err := sut.Resolve(command)
t.Run("Then an error is returned", func(t *testing.T) {
isRequire.Equal(CommandHandler{}, handler)
isRequire.Error(err)
})
})
t.Run("When a command with its handler is added", func(t *testing.T) {
handler := &ACommandHandler{}
sut.AddHandler(handler.Handle)
t.Run("Then the handler is resolved", func(t *testing.T) {
resolvedHandler, err := sut.Resolve(command)
isRequire.NoError(err)
isRequire.Equal(
NewCommandHandler(handler.Handle).CommandName(),
resolvedHandler.CommandName(),
)
})
})
})
}
func TestCromberBus_Dispatch(t *testing.T) {
isRequire := require.New(t)
t.Run("Given a cromberbus command bus without middlewares", func(t *testing.T) {
handler := ACommandHandler{}
handlerResolver := CommandHandlerResolverMock{
ResolveFunc: func(command Command) (CommandHandler, error) {
return NewCommandHandler(handler.Handle), nil
},
}
sut := NewCromberBus(&handlerResolver)
t.Run("When a command is dispatched", func(t *testing.T) {
command := struct{}{}
_ = sut.Dispatch(command)
t.Run("Then the resolved command handler handles the command", func(t *testing.T) {
resolverHasBeenCalled := len(handlerResolver.ResolveCalls()) > 0
isRequire.True(resolverHasBeenCalled)
handlerHasBeenCalled := handler.NumberOfHandleCalls > 0
isRequire.True(handlerHasBeenCalled)
})
})
})
t.Run("Given a cromberbus command bus with middlewares", func(t *testing.T) {
aMiddleware := &AMiddleware{}
anotherMiddleware := &AMiddleware{}
handler := ACommandHandler{}
handlerResolver := CommandHandlerResolverMock{
ResolveFunc: func(command Command) (CommandHandler, error) {
return NewCommandHandler(handler.Handle), nil
},
}
sut := NewCromberBus(&handlerResolver, aMiddleware, anotherMiddleware)
t.Run("When a command is dispatched", func(t *testing.T) {
command := struct{}{}
_ = sut.Dispatch(command)
t.Run("Then the resolved command handler handles the command", func(t *testing.T) {
resolverHasBeenCalled := len(handlerResolver.ResolveCalls()) > 0
isRequire.True(resolverHasBeenCalled)
handlerHasBeenCalled := handler.NumberOfHandleCalls > 0
isRequire.True(handlerHasBeenCalled)
})
t.Run("And the middlewares are executed", func(t *testing.T) {
isRequire.True(aMiddleware.NumberOfExecuteCalls > 0)
isRequire.True(anotherMiddleware.NumberOfExecuteCalls > 0)
})
})
})
}