Inheritance: AggregateRoot
        public void NoEventsExecutedIfNoAppliedEventsExist()
        {
            TestAggregateRoot aggregateRoot = new TestAggregateRoot(Guid.NewGuid());
            sut.ApplyChangesAsync(aggregateRoot).Wait();

            Assert.AreEqual(0, sut.EventBroker.ExecutedEvents.Count);
        }
        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);
        }
        public void AllAppliedEventsAreExecuted()
        {
            TestAggregateRoot aggregateRoot = new TestAggregateRoot(Guid.NewGuid());
            var domainEvent1 = ApplyNewEvent(aggregateRoot);
            var domainEvent2 = ApplyNewEvent(aggregateRoot);

            sut.ApplyChangesAsync(aggregateRoot).Wait();

            Assert.AreEqual(2, sut.EventBroker.ExecutedEvents.Count);
            Assert.IsTrue(sut.EventBroker.ExecutedEvents.Contains(domainEvent1));
            Assert.IsTrue(sut.EventBroker.ExecutedEvents.Contains(domainEvent2));
        }
        public void ResolvedEventHandlersAreExecutedInCorrectOrder()
        {
            TestAggregateRoot aggregateRoot = new TestAggregateRoot(Guid.NewGuid());
            aggregateRoot.ApplyEvent(domainEvent);

            sut.ApplyChangesAsync(aggregateRoot).Wait();

            Received.InOrder(() =>
            {
                domainEventHandler1.ExecuteAsync(domainEvent);
                domainEventHandler2.ExecuteAsync(domainEvent);
            });
        }
        public void AllAppliedEventsForAllAggregateRootsAreExecuted()
        {
            TestAggregateRoot aggregateRoot1 = new TestAggregateRoot(Guid.NewGuid());
            var domainEvent1 = ApplyNewEvent(aggregateRoot1);
            var domainEvent2 = ApplyNewEvent(aggregateRoot1);

            TestAggregateRoot aggregateRoot2 = new TestAggregateRoot(Guid.NewGuid());
            var domainEvent3 = ApplyNewEvent(aggregateRoot2);
            var domainEvent4 = ApplyNewEvent(aggregateRoot2);

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

            Assert.AreEqual(4, sut.EventBroker.ExecutedEvents.Count);
            Assert.IsTrue(sut.EventBroker.ExecutedEvents.Contains(domainEvent1));
            Assert.IsTrue(sut.EventBroker.ExecutedEvents.Contains(domainEvent2));
            Assert.IsTrue(sut.EventBroker.ExecutedEvents.Contains(domainEvent3));
            Assert.IsTrue(sut.EventBroker.ExecutedEvents.Contains(domainEvent4));
        }
        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 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();
            });
        }
        public void ChangesInUnitOfWorkAreSaved()
        {
            TestAggregateRoot aggregateRoot = new TestAggregateRoot(Guid.NewGuid());
            aggregateRoot.ApplyEvent(domainEvent);

            sut.ApplyChanges(aggregateRoot);

            unitOfWork.Received().SaveChanges();
        }
 public void VersionIsSet()
 {
     sut = new TestAggregateRoot(id, 10);
     Assert.AreEqual(10, sut.Version);
 }
 public void IdCannotBeDefaultEmpty()
 {
     sut = new TestAggregateRoot(Guid.Empty);
 }
 public void Setup()
 {
     id = Guid.NewGuid();
     sut = new TestAggregateRoot(id);
 }
        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);
        }
 private DomainEvent ApplyNewEvent(TestAggregateRoot aggregateRoot)
 {
     var domainEvent = new TestDomainEvent(Guid.NewGuid());
     aggregateRoot.ApplyEvent(domainEvent);
     return domainEvent;
 }