public void EventHandlersAreExecutedWithSingleEvent()
        {
            sut.PublishEvent(testEvent1);

            Received.InOrder(() =>
            {
                domainEventHandler1.Received().Execute(testEvent1);
                domainEventHandler2.Received().Execute(testEvent1);
                asyncDomainEventHandler1.Received().ExecuteAsync(testEvent1);
                asyncDomainEventHandler2.Received().ExecuteAsync(testEvent1);
            });

            domainEventHandler3.DidNotReceive().Execute(Arg.Any <AnotherTestDomainEvent>());
            asyncDomainEventHandler3.DidNotReceive().ExecuteAsync(Arg.Any <AnotherTestDomainEvent>());
        }
Exemple #2
0
        public void OnlyUnitOfWorkEventsAreHandledIfCommitFails()
        {
            TestAggregateRoot aggregateRoot = new TestAggregateRoot(Guid.NewGuid());

            aggregateRoot.ApplyEvent(domainEvent);

            unitOfWork
            .When(u => u.SaveChanges())
            .Do(callInfo => { throw new InvalidOperationException(); });

            try
            {
                sut.ApplyChanges(aggregateRoot);
                Assert.Fail("No exception thrown.");
            }
            catch (InvalidOperationException)
            {
            }

            unitOfWorkDomainEventHandler1.Received().Execute(domainEvent, unitOfWork);
            unitOfWorkDomainEventHandler2.Received().Execute(domainEvent, unitOfWork);
            unitOfWork.Received().SaveChanges();
            domainEventHandler3.DidNotReceive().Execute(domainEvent);
            domainEventHandler4.DidNotReceive().Execute(domainEvent);
        }
        public void OnlyUnitOfWorkEventsAreHandledIfCommitFails()
        {
            TestAggregateRoot aggregateRoot = new TestAggregateRoot(Guid.NewGuid());

            aggregateRoot.ApplyEvent(domainEvent1);

            TestAggregateRoot2 aggregateRoot2 = new TestAggregateRoot2(Guid.NewGuid());

            aggregateRoot.ApplyEvent(domainEvent2);

            unitOfWork
            .When(u => u.SaveChanges())
            .Do(callInfo => { throw new OperationCanceledException(); });

            bool exceptionCatched = false;

            try
            {
                sut.ApplyChanges(aggregateRoot, aggregateRoot2);
            }
            catch (OperationCanceledException)
            {
                exceptionCatched = true;
            }

            Assert.IsTrue(exceptionCatched);
            unitOfWorkDomainEventHandler1.Received().Execute(domainEvent1, unitOfWork);
            unitOfWorkDomainEventHandler2.Received().Execute(domainEvent1, unitOfWork);
            unitOfWorkDomainEventHandler1.Received().Execute(domainEvent2, unitOfWork);
            unitOfWorkDomainEventHandler2.Received().Execute(domainEvent2, unitOfWork);
            unitOfWork.Received().SaveChanges();
            domainEventHandler3.DidNotReceive().Execute(domainEvent1);
            domainEventHandler4.DidNotReceive().Execute(domainEvent1);
            domainEventHandler3.DidNotReceive().Execute(domainEvent2);
            domainEventHandler4.DidNotReceive().Execute(domainEvent2);
        }