public void ResolvedEventHandlersAreExecutedInCorrectOrder()
        {
            TestAggregateRoot aggregateRoot = new TestAggregateRoot(Guid.NewGuid());

            aggregateRoot.ApplyEvent(domainEvent1);

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

            aggregateRoot.ApplyEvent(domainEvent2);

            sut.ApplyChanges(aggregateRoot, aggregateRoot2);

            Received.InOrder(() =>
            {
                unitOfWorkDomainEventHandler1.Execute(domainEvent1, unitOfWork);
                unitOfWorkDomainEventHandler2.Execute(domainEvent1, unitOfWork);
                unitOfWorkDomainEventHandler1.Execute(domainEvent2, unitOfWork);
                unitOfWorkDomainEventHandler2.Execute(domainEvent2, unitOfWork);
                unitOfWork.SaveChanges();
                domainEventHandler3.Execute(domainEvent1);
                domainEventHandler4.Execute(domainEvent1);
                domainEventHandler3.Execute(domainEvent2);
                domainEventHandler4.Execute(domainEvent2);
            });
        }
        public void ChangesInUnitOfWorkAreSavedOnce()
        {
            TestAggregateRoot aggregateRoot = new TestAggregateRoot(Guid.NewGuid());

            aggregateRoot.ApplyEvent(domainEvent1);

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

            aggregateRoot.ApplyEvent(domainEvent2);

            sut.ApplyChanges(aggregateRoot, aggregateRoot2);

            unitOfWork.Received(1).SaveChanges();
        }
Пример #3
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);
        }
Пример #4
0
        public void MultipleChangesInUnitOfWorkAreSavedOnce()
        {
            TestAggregateRoot aggregateRoot1 = new TestAggregateRoot(Guid.NewGuid());

            aggregateRoot1.ApplyEvent(domainEvent);

            TestDomainEvent domainEvent2 = new TestDomainEvent(Guid.NewGuid());

            unitOfWorkEventHandlerResolver.ResolveEventHandlers <IUnitOfWorkDomainEventHandler <TestDomainEvent, TestUnitOfWork> >().Returns(unitOfWorkDomainEventHandlers);

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

            aggregateRoot2.ApplyEvent(domainEvent2);

            sut.ApplyChanges(new List <TestAggregateRoot> {
                aggregateRoot1, aggregateRoot2
            });

            Received.InOrder(() =>
            {
                unitOfWorkDomainEventHandler1.Execute(domainEvent, unitOfWork);
                unitOfWorkDomainEventHandler1.Execute(domainEvent2, unitOfWork);
                unitOfWork.SaveChanges();
            });
        }
        private DomainEvent ApplyNewEvent(TestAggregateRoot aggregateRoot)
        {
            var domainEvent = new TestDomainEvent(Guid.NewGuid());

            aggregateRoot.ApplyEvent(domainEvent);
            return(domainEvent);
        }
        public void OnlyUnitOfWorkEventsAreHandledIfCommitFails()
        {
            TestAggregateRoot aggregateRoot = new TestAggregateRoot(Guid.NewGuid());

            aggregateRoot.ApplyEvent(domainEvent);

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

            try
            {
                sut.ApplyChangesAsync(aggregateRoot).Wait();
                Assert.Fail("No exception thrown.");
            }
            catch (AggregateException e)
            {
                if (e.InnerException == null)
                {
                    Assert.Fail("Expected inner exception of type AggregateException. No inner exception received.");
                }

                if (!(e.InnerException is InvalidOperationException))
                {
                    Assert.Fail("Expected inner exception of type AggregateException. Received '{0}'.", e.InnerException);
                }
            }

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

            aggregateRoot.ApplyEvent(domainEvent);

            sut.ApplyChangesAsync(aggregateRoot).Wait();

            unitOfWork.Received().SaveChanges();
        }
Пример #8
0
        public void ResolvedUnitOfWorkEventHandlersAreExecutedInCorrectOrder()
        {
            TestAggregateRoot aggregateRoot = new TestAggregateRoot(Guid.NewGuid());

            aggregateRoot.ApplyEvent(domainEvent);

            sut.ApplyChanges(aggregateRoot);

            unitOfWorkDomainEventHandler1.Received().Execute(domainEvent, unitOfWork);
            unitOfWorkDomainEventHandler2.Received().Execute(domainEvent, unitOfWork);
        }
Пример #9
0
        public void AppliedEventIsReturned()
        {
            var domainEvent = new TestDomainEvent(Guid.NewGuid());

            sut.ApplyEvent(domainEvent);
            IEnumerable <IDomainEvent> appliedEvents = sut.GetAppliedEvents();

            Assert.AreEqual(1, appliedEvents.Count());
            Assert.AreSame(domainEvent, appliedEvents.Single());
        }
        public void ResolvedEventHandlersAreExecutedInCorrectOrder()
        {
            TestAggregateRoot aggregateRoot = new TestAggregateRoot(Guid.NewGuid());

            aggregateRoot.ApplyEvent(domainEvent);

            sut.ApplyChanges(aggregateRoot);

            Received.InOrder(() =>
            {
                domainEventHandler1.Execute(domainEvent);
                domainEventHandler2.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);
        }
        public void ResolvedEventHandlersAreExecutedInCorrectOrder()
        {
            TestAggregateRoot aggregateRoot = new TestAggregateRoot(Guid.NewGuid());

            aggregateRoot.ApplyEvent(domainEvent);

            sut.ApplyChangesAsync(aggregateRoot).Wait();

            Received.InOrder(() =>
            {
                unitOfWorkDomainEventHandler1.ExecuteAsync(domainEvent, unitOfWork);
                unitOfWorkDomainEventHandler2.ExecuteAsync(domainEvent, unitOfWork);
                domainEventHandler3.ExecuteAsync(domainEvent);
                domainEventHandler4.ExecuteAsync(domainEvent);
            });
        }