@@ -90,7 +90,7 @@ public async Task GetEntityAsync_ThenInclude_NotNullAsync()
9090 }
9191
9292 [ Fact ]
93- public async Task SaveEntitiesAsync_CallBeforeUpdateForRelatedEntityAsync ( )
93+ public async Task SaveEntitiesAsync_CallBeforeUpdateForRelatedEntity_UpdateDateUpdatedAsync ( )
9494 {
9595 // Arrange
9696 var entity = new EntityGenerator < FakeBlog > ( new FakeBlog ( ) )
@@ -118,7 +118,7 @@ public async Task SaveEntitiesAsync_CallBeforeUpdateForRelatedEntityAsync()
118118 }
119119
120120 [ Fact ]
121- public async Task SaveEntitiesAsync_DispatchEntityDomainEventsAsync ( )
121+ public async Task SaveEntitiesAsync_DispatchEntityDomainEvents_DispatchAllAsync ( )
122122 {
123123 // Arrange
124124 var entity = new EntityGenerator < FakeBlog > ( new FakeBlog ( ) )
@@ -156,7 +156,7 @@ public async Task SaveEntitiesAsync_DispatchEntityDomainEventsAsync()
156156 }
157157
158158 [ Fact ]
159- public async Task SaveEntitiesAsync_DispatchRelatedEntityDomainEventsAsync ( )
159+ public async Task SaveEntitiesAsync_DispatchRelatedEntityDomainEvents_DispatchAllAsync ( )
160160 {
161161 // Arrange
162162 var entity = new EntityGenerator < FakeBlog > ( new FakeBlog ( ) )
@@ -195,7 +195,7 @@ public async Task SaveEntitiesAsync_DispatchRelatedEntityDomainEventsAsync()
195195 }
196196
197197 [ Fact ]
198- public async Task SaveEntitiesAsync_DispatchEntityDomainEventsWithGeneratedIdAsync ( )
198+ public async Task SaveEntitiesAsync_DispatchEntityDomainEventsWithGeneratedId_DispatchAllAsync ( )
199199 {
200200 // Arrange
201201 var entity = new EntityGenerator < FakeBlog > ( new FakeBlog ( ) )
@@ -228,4 +228,44 @@ public async Task SaveEntitiesAsync_DispatchEntityDomainEventsWithGeneratedIdAsy
228228 It . IsAny < CancellationToken > ( ) ) ,
229229 Times . Exactly ( entity . Posts . Count ) ) ;
230230 }
231+
232+ [ Fact ]
233+ public async Task SaveEntitiesAsync_DispatchEntityDomainEventsWithMultipleExceptions_ThrowAggregateExceptionsAsync ( )
234+ {
235+ // Arrange
236+ var entity = new EntityGenerator < FakeBlog > ( new FakeBlog ( ) )
237+ . Setup ( x => x . DateUpdated = DateTimeOffset . Now . AddDays ( - 1 ) )
238+ . HasManyForEachEntity (
239+ x => x . Posts ,
240+ x => x . Blog ,
241+ new EntityGenerator < FakePost > ( new FakePost ( ) ) . Setup (
242+ x => x . DateUpdated = DateTimeOffset . Now . AddDays ( - 1 ) ) )
243+ . GenerateSingle ( ) ;
244+ var db = new FakeDbContext (
245+ new DbContextOptionsBuilder < FakeDbContext > ( ) . UseInMemoryDatabase ( "inmemory" ) . Options ) ;
246+ var mediator = new Mock < IMediator > ( ) ;
247+ mediator . Setup ( x => x . Publish ( It . IsAny < IDomainEvent > ( ) , It . IsAny < CancellationToken > ( ) ) )
248+ . Throws < ArgumentException > ( ) ;
249+ var repository = new TestRepository ( mediator . Object , db ) ;
250+
251+ // Act
252+ entity . AddDomainEvent ( id => new FakeDomainEvent ( id , 1 ) ) ;
253+ entity . Posts . ForEach ( x => x . AddDomainEvent ( id => new FakeDomainEvent ( id , 2 ) ) ) ;
254+ var act = async ( ) => await repository . AddAsync ( entity ) ;
255+
256+ // Assert
257+ var eventCount = 1 + entity . Posts . Count ;
258+ ( await act . Should ( ) . ThrowAsync < AggregateException > ( ) ) . And . InnerExceptions . Should ( )
259+ . HaveCount ( eventCount ) ;
260+ mediator . Verify (
261+ x => x . Publish (
262+ It . Is < IDomainEvent > ( d => ( ( FakeDomainEvent ) d ) . Id != 0 && ( ( FakeDomainEvent ) d ) . FakeValue == 1 ) ,
263+ It . IsAny < CancellationToken > ( ) ) ,
264+ Times . Once ) ;
265+ mediator . Verify (
266+ x => x . Publish (
267+ It . Is < IDomainEvent > ( d => ( ( FakeDomainEvent ) d ) . Id != 0 && ( ( FakeDomainEvent ) d ) . FakeValue == 2 ) ,
268+ It . IsAny < CancellationToken > ( ) ) ,
269+ Times . Exactly ( entity . Posts . Count ) ) ;
270+ }
231271}
0 commit comments