예제 #1
0
        public async Task GetByIdAsyncWithEventsShouldReturnTheCurrentStateOfTheAggregate()
        {
            //Arrange

            #region mock of DataBaseContext , mock of IDomainEventRebuilder and mock of moqInvoker

            var aggregateId    = Guid.NewGuid();
            var optionsBuilder = new DbContextOptionsBuilder <DataBaseContext>();
            optionsBuilder.UseInMemoryDatabase("FakeInMemoryData");
            var moqContext = new DataBaseContext(optionsBuilder.Options);
            moqContext.Database.EnsureCreated();

            var obj  = new EventOject(aggregateId, "Dupont", "45 av charles degaulle paris, france");
            var type = obj.GetType().AssemblyQualifiedName;
            var json = @"
                {
                    'FullName':'Dupont',
                    'Adresse':'45 av charles degaulle paris, france'
                }";

            moqContext.EventStore.Add(new EventStore(aggregateId,
                                                     1,
                                                     "2@735f8407-16be-44b5-be96-2bab582b5298",
                                                     type,
                                                     DateTime.Now, json));
            moqContext.SaveChanges();

            Mock <IDomainEventRebuilder> moqDomainEventRebuilder = new Mock <IDomainEventRebuilder>();
            moqDomainEventRebuilder.Setup(m => m.RebuildDomainEvents(It.IsAny <IEnumerable <EventStore> >()))
            .Returns(new List <Event>
            {
                obj
            });

            var aggregate = (StubAggregate)typeof(StubAggregate)
                            .GetConstructor(BindingFlags.Instance | BindingFlags.NonPublic,
                                            null,
                                            new Type[0],
                                            new ParameterModifier[0])
                            ?.Invoke(new object[0]);

            var moqInvoker = new Mock <IInvoker <StubAggregate> >();
            moqInvoker.Setup(i => i.CreateInstanceOfAggregateRoot <StubAggregate>()).Returns(aggregate);

            #endregion mock of DataBaseContext , mock of IDomainEventRebuilder and mock of moqInvoker

            var sut = new EventStoreRepository <StubAggregate>(moqContext, moqInvoker.Object, moqDomainEventRebuilder.Object);

            //Act
            var result = await sut.GetByIdAsync <StubAggregate>(aggregateId);

            moqContext.Dispose();

            //Assert
            Assert.NotNull(aggregate);
            Assert.Equal(aggregate, result);
            Assert.Equal(aggregateId, aggregate.Id);
            Assert.Equal(obj.FullName, aggregate.FullName);
            Assert.Equal(obj.Adresse, aggregate.Adresse);
        }
예제 #2
0
 public void Apply(EventOject eventOject)
 {
     Id       = eventOject.AggregateId;
     FullName = eventOject.FullName;
     Adresse  = eventOject.Adresse;
 }