public async Task ExistingEventsWithSameAggregateIdAreAppliedToSameAggregate()
        {
            var createdEvent = new TestAggregateCreatedEvent(new Guid("b166fbb3-b7f9-49d2-8a61-0b4df5fff561"), "Test1_Initial");
            var updatedEvent = new TestAggregateUpdatedEvent(new Guid("b166fbb3-b7f9-49d2-8a61-0b4df5fff561"), "Test1_Updated");

            A.CallTo(() => _persistenceClient.Retrieve(typeof(TestAggregateCreatedEvent)))
            .Returns(new IEvent[] { createdEvent });
            A.CallTo(() => _persistenceClient.Retrieve(typeof(TestAggregateUpdatedEvent)))
            .Returns(new IEvent[] { updatedEvent });

            var reader = new TestAggregateReader(_persistenceClient, _streamingClient);
            await reader.StartAsync();

            var aggregates = reader.Get();

            Assert.AreEqual(1, aggregates.Length);

            var actualAggregate = aggregates[0];

            Assert.AreEqual(updatedEvent.Foo, actualAggregate.Foo);
        }
        public async Task OnSaveIfSaveFailsEventIsNotPublished()
        {
            A.CallTo(() => _persistenceClient.Save(A <string> .Ignored, A <int> .Ignored, A <IEvent> .Ignored))
            .Throws(new Exception("Saving failed"));

            var writer = new TestAggregateWriter(_persistenceClient, _streamingClient);
            await writer.StartAsync();

            var newUpdatedEvent = new TestAggregateUpdatedEvent(new Guid("b166fbb3-b7f9-49d2-8a61-0b4df5fff561"), "Test1_Updated_SecondTime");

            try
            {
                await writer.Save(newUpdatedEvent);
            }
            catch (Exception)
            {
                // ignored
            }

            A.CallTo(() => _streamingClient.Publish(A <IEvent> .Ignored))
            .MustNotHaveHappened();
        }
        public async Task WhenSavingNewEventUsesVersionOfAggregateAsExpectedVersion()
        {
            var createdEvent = new TestAggregateCreatedEvent(new Guid("b166fbb3-b7f9-49d2-8a61-0b4df5fff561"), "Test1_Initial");
            var updatedEvent = new TestAggregateUpdatedEvent(new Guid("b166fbb3-b7f9-49d2-8a61-0b4df5fff561"), "Test1_Updated");

            A.CallTo(() => _persistenceClient.Retrieve(typeof(TestAggregateCreatedEvent)))
            .Returns(new IEvent[] { createdEvent });
            A.CallTo(() => _persistenceClient.Retrieve(typeof(TestAggregateUpdatedEvent)))
            .Returns(new IEvent[] { updatedEvent });

            var writer = new TestAggregateWriter(_persistenceClient, _streamingClient);
            await writer.StartAsync();

            var newUpdatedEvent = new TestAggregateUpdatedEvent(new Guid("b166fbb3-b7f9-49d2-8a61-0b4df5fff561"), "Test1_Updated_SecondTime");

            await writer.Save(newUpdatedEvent);

            A.CallTo(() => _persistenceClient.Save(
                         "TestAggregateRoot_b166fbb3-b7f9-49d2-8a61-0b4df5fff561",
                         1,
                         A <IEvent> .Ignored))
            .MustHaveHappenedOnceExactly();
        }
 private void When(TestAggregateUpdatedEvent updatedEvent)
 {
     Foo = updatedEvent.Foo;
 }