Skip to content

Commit 17ef28b

Browse files
Updated test to use new mocks
1 parent 0e81f65 commit 17ef28b

File tree

10 files changed

+299
-335
lines changed

10 files changed

+299
-335
lines changed

aggregate/aggregate_test.go

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@ package aggregate_test
55
import (
66
"testing"
77

8+
"github.com/golang/mock/gomock"
9+
810
"github.com/hellofresh/goengine/aggregate"
9-
"github.com/hellofresh/goengine/mocks"
11+
mocks "github.com/hellofresh/goengine/mocks/aggregate"
1012
"github.com/stretchr/testify/assert"
11-
"github.com/stretchr/testify/mock"
1213
)
1314

1415
func TestGenerateID(t *testing.T) {
@@ -86,28 +87,26 @@ func TestIDFromString(t *testing.T) {
8687

8788
func TestRecordChange(t *testing.T) {
8889
t.Run("A change is recorded", func(t *testing.T) {
90+
asserts := assert.New(t)
91+
ctrl := gomock.NewController(t)
92+
defer ctrl.Finish()
93+
8994
rootID := aggregate.GenerateID()
9095
domainEvent := struct{}{}
9196

92-
root := &mocks.AggregateRoot{}
93-
root.On("AggregateID").Return(rootID)
94-
root.On("Apply", mock.AnythingOfType("*aggregate.Changed"))
97+
root := mocks.NewRoot(ctrl)
98+
root.EXPECT().AggregateID().Return(rootID)
99+
root.EXPECT().Apply(gomock.AssignableToTypeOf(&aggregate.Changed{})).Do(func(msg *aggregate.Changed) {
100+
asserts.Equal(rootID, msg.AggregateID())
101+
asserts.Equal(domainEvent, msg.Payload())
102+
asserts.Equal(uint(1), msg.Version())
103+
}).Times(1)
95104

96105
// Record the change
97106
err := aggregate.RecordChange(root, domainEvent)
98107

99108
// Check that the change was recorded
100-
asserts := assert.New(t)
101109
asserts.Empty(err, "No error should be returned")
102-
103-
root.AssertExpectations(t)
104-
calls := mocks.FetchFuncCalls(root.Calls, "Apply")
105-
if asserts.Len(calls, 1) {
106-
msg := calls[0].Arguments[0].(*aggregate.Changed)
107-
asserts.Equal(rootID, msg.AggregateID())
108-
asserts.Equal(domainEvent, msg.Payload())
109-
asserts.Equal(uint(1), msg.Version())
110-
}
111110
})
112111

113112
t.Run("Check required arguments", func(t *testing.T) {
@@ -133,17 +132,17 @@ func TestRecordChange(t *testing.T) {
133132

134133
for _, testCase := range errorTestCases {
135134
t.Run(testCase.title, func(t *testing.T) {
136-
root := &mocks.AggregateRoot{}
137-
root.On("AggregateID").Return(testCase.aggregateID)
138-
root.On("Apply", mock.AnythingOfType("*aggregate.Changed"))
135+
ctrl := gomock.NewController(t)
136+
defer ctrl.Finish()
137+
138+
root := mocks.NewRoot(ctrl)
139+
root.EXPECT().AggregateID().Return(testCase.aggregateID)
139140

140141
// Record the change
141142
err := aggregate.RecordChange(root, testCase.domainEvent)
142143

143144
// Check error
144-
asserts := assert.New(t)
145-
asserts.Equal(testCase.expectedError, err)
146-
root.AssertNotCalled(t, "Apply", mock.Anything)
145+
assert.Equal(t, testCase.expectedError, err)
147146
})
148147
}
149148
})

aggregate/aggregate_type_test.go

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,16 @@ import (
77

88
"github.com/hellofresh/goengine"
99
"github.com/hellofresh/goengine/aggregate"
10-
"github.com/hellofresh/goengine/mocks"
10+
mocks "github.com/hellofresh/goengine/mocks/aggregate"
1111
"github.com/stretchr/testify/assert"
1212
)
1313

1414
func TestType(t *testing.T) {
1515
mockInitiator := func() aggregate.Root {
16-
return &mocks.AggregateRoot{}
16+
return &mocks.Root{}
1717
}
1818

1919
t.Run("Create a new type", func(t *testing.T) {
20-
t.Parallel()
21-
2220
aggregateType, err := aggregate.NewType("mock", mockInitiator)
2321

2422
asserts := assert.New(t)
@@ -28,7 +26,7 @@ func TestType(t *testing.T) {
2826
asserts.NotEmpty(aggregateType, "A aggregate.Type should be returned")
2927
asserts.Equal("mock", aggregateType.String(), "Expected name to be set")
3028
asserts.True(
31-
aggregateType.IsImplementedBy(&mocks.AggregateRoot{}),
29+
aggregateType.IsImplementedBy(&mocks.Root{}),
3230
"Expected the same aggregate type to be valid",
3331
)
3432

@@ -38,11 +36,11 @@ func TestType(t *testing.T) {
3836
}{
3937
{
4038
title: "another aggregate is not of this type",
41-
value: &mocks.AnotherAggregateRoot{},
39+
value: &mocks.AnotherRoot{},
4240
},
4341
{
4442
title: "not a reference is not of this type",
45-
value: mocks.AggregateRoot{},
43+
value: mocks.Root{},
4644
},
4745
{
4846
title: "nil is not of this type",
@@ -98,7 +96,7 @@ func TestType(t *testing.T) {
9896
expectedError: aggregate.ErrInitiatorMustReturnRoot,
9997
name: "init",
10098
initiator: func() aggregate.Root {
101-
return (*mocks.AggregateRoot)(nil)
99+
return (*mocks.Root)(nil)
102100
},
103101
},
104102
}

0 commit comments

Comments
 (0)