@@ -5,10 +5,11 @@ package aggregate_test
55import (
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
1415func TestGenerateID (t * testing.T ) {
@@ -86,28 +87,26 @@ func TestIDFromString(t *testing.T) {
8687
8788func 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 })
0 commit comments