public void Should_Invoke_Registered_Domain_Event_Handler() { TestDomainEventHandler handler = new TestDomainEventHandler(_testOutput); IEventSourcedAggregateRepository <TestAggregate, Guid> repository = Factory.CreateTestAggregateRepository(reg => { reg.Register <TestAggregateCreatedEvent>(() => handler); reg.Register <OperationExecutedEvent>(() => handler); }); TestAggregate aggregate = new TestAggregate(Guid.NewGuid()); aggregate.ExecuteSomeOperation("Test Operation"); repository.Save(aggregate); // Event may not have yet been handled in background. Thread.Sleep(1000); // Aggregate should be stored. TestAggregate storedAggregate = repository.GetById(aggregate.Id); Assert.Equal(aggregate.Id, storedAggregate.Id); // Aggregate should have 2 events. // 1. TestAggregateCreated // 2. TestAggregateModified Assert.Equal(2, handler.HandledEvents.Count); Assert.Contains(handler.HandledEvents, (e) => e is TestAggregateCreatedEvent); Assert.Contains(handler.HandledEvents, (e) => e is OperationExecutedEvent); }
public void Should_Trigger_OnError_When_Domain_Event_Handler_Exception_Occurred() { TestDomainEventHandler handler = new TestDomainEventHandler(_testOutput); IEventPublisher publisher = Factory.CreatePublisher(reg => { reg.Register <TestAggregateCreatedEvent>(() => handler); reg.Register <OperationExecutedEvent>(() => handler); }); publisher.OnError += (e, ex) => { _testOutput.WriteLine($"{ex.GetType().Name} occurred while handling {e.GetType().Name} event: {ex.Message}"); Assert.IsType <TestAggregateDomainEventHandlerException>(ex); }; IEventSourcedAggregateRepository <TestAggregate, Guid> repository = Factory.CreateTestAggregateRepository(publisher); TestAggregate aggregate = new TestAggregate(Guid.NewGuid()); // This would trigger a TestAggregateDomainEventHandlerException when handled by TestDomainEventHandler. aggregate.TriggerExceptionOnEventHandler(); repository.Save(aggregate); }
public void ExecuteDeleteCmdApplicationConfigurationCommandWithNullThrowsException( CmdApplicationConfigurationService sut, TestDomainEventHandler<ConfigurationDeletedEvent> deleteHandler, DeleteCmdApplicationConfigurationCommand command) { DomainEvents.Subscribe(deleteHandler); sut.Execute(command); Assert.True(deleteHandler.EventHandlerInvoked); }
public void ExecuteSaveCmdApplicationConfigurationWithExistingNameRaisesRejectedEvent( [Frozen]Mock<ICmdApplicationConfigurationRepository> repository, SaveCmdApplicationConfigurationCommand command, TestDomainEventHandler<CmdApplicationConfigurationSaveRejected> testHandler, CmdApplicationConfigurationService sut) { repository .Setup(a => a.CheckIfConfigurationWithSameNameExists(command.ApplicationConfiguration)) .Returns(true); DomainEvents.Subscribe(testHandler); sut.Execute(command); Assert.True(testHandler.EventHandlerInvoked); }