Exemplo n.º 1
0
        public async Task ShouldCloseBet()
        {
            var      betId                = Guid.NewGuid();
            var      memberId             = new MemberId(Guid.Parse("1311e8c9-d993-4346-94c1-1907e3ce65d3"));
            var      creator              = new Member(memberId, "name", 300);
            BetState betState             = GenerateBet(betId, creator);
            var      domainEventsListener = new DomainEventsAccessor();
            var      repository           = new InMemoryBetRepository(domainEventsListener, betState);
            var      command              = new CloseBetCommand(betId, true);
            var      dateTimeClosed       = new DateTime(2021, 4, 1);
            var      dateTimeProvider     = new FakeDateTimeProvider(dateTimeClosed);
            var      handler              = new CloseBetCommandHandler(repository, dateTimeProvider, new InMemoryAuthenticationGateway(true, memberId.Value));

            await handler.Handle(command, default);

            var bet = await repository.GetByIdAsync(new(betId));

            Assert.True(bet.State.IsSuccess);
            Assert.Equal(dateTimeClosed, bet.State.CloseDate);
            var betClosedEvent = domainEventsListener.GetDomainEvents()
                                 .SingleOrDefault(x => x.GetType() == typeof(BetClosed)) as BetClosed;
            var betUpdatedEvent = domainEventsListener.GetDomainEvents()
                                  .SingleOrDefault(x => x.GetType() == typeof(BetUpdated)) as BetUpdated;

            Assert.NotNull(betClosedEvent);
            Assert.NotNull(betUpdatedEvent);
            Assert.Equal(betId, betClosedEvent.BetId);
            Assert.True(betClosedEvent.IsSuccess);
            Assert.Equal(betId, betUpdatedEvent.BetId);
        }
Exemplo n.º 2
0
        private void AssertThatUserIsRegistered(string token, string actualToken)
        {
            var user        = new User("abc", "username", "*****@*****.**", "password", new DateTime(2021, 9, 22));
            var currentUser = userRepository.GetUsers().Single();

            Assert.Equal(user, currentUser);
            Assert.Equal(user.RegisterDate, currentUser.RegisterDate);
            var domainEvent = domainEventsAccessor.GetDomainEvents().SingleOrDefault();

            Assert.Equal(new UserRegistered("abc", "*****@*****.**", "username"), domainEvent);
            Assert.Equal(token, actualToken);
            Assert.Collection(domainEventsAccessor.GetDomainEvents(), x =>
            {
                Assert.Equal("abc", (x as UserRegistered).Id);
                Assert.Equal("username", (x as UserRegistered).Username);
                Assert.Equal("*****@*****.**", (x as UserRegistered).Email);
            });
        }
Exemplo n.º 3
0
        public async Task ShouldCreateBet()
        {
            //arrange
            IDateTimeProvider dtNow  = new FakeDateTimeProvider(new DateTime(2021, 5, 6, 0, 0, 0));
            var endDate              = new DateTime(2021, 5, 7, 0, 0, 0);
            var command              = new LaunchBetCommand(_betId, endDate, coins, description);
            var domainEventsListener = new DomainEventsAccessor();
            var betRepository        = new InMemoryBetRepository(domainEventsListener);
            var member           = new Member(new MemberId(_creatorId), "name", 25);
            var memberRepository = new InMemoryMemberRepository(new List <Member>()
            {
                member
            });
            var      handler     = new LaunchBetCommandHandler(betRepository, memberRepository, new InMemoryAuthenticationGateway(true, _creatorId), dtNow);
            BetState expectedBet = new(_betId, member, endDate, description, coins, dtNow.Now, new List <AnswerState>(), null, null);

            //act
            await handler.Handle(command, default);

            //assert
            Bet actualBet = await betRepository.GetByIdAsync(new(_betId));

            IDomainEvent domainEvent = domainEventsListener.GetDomainEvents()
                                       .SingleOrDefault(x => x.GetType() == typeof(BetCreated));

            Assert.Equal(expectedBet.BetId, actualBet.State.BetId);
            Assert.Equal(expectedBet.Description, actualBet.State.Description);
            Assert.Equal(expectedBet.EndDate, actualBet.State.EndDate);
            Assert.Equal(expectedBet.Creator.Id, actualBet.State.Creator.Id);
            Assert.Equal(expectedBet.Creator.Name, actualBet.State.Creator.Name);
            Assert.Equal(expectedBet.Creator.Wallet, actualBet.State.Creator.Wallet);
            Assert.Equal(expectedBet.Coins, actualBet.State.Coins);
            Assert.Equal(expectedBet.CreationDate, actualBet.State.CreationDate);
            Assert.False(actualBet.IsClosed());
            Assert.True(expectedBet.EndDate > actualBet.State.CreationDate);
            Assert.True(actualBet.GetEndDateToAnswer().CompareTo(new DateTime(2021, 5, 6, 6, 0, 0)) == 0);
            Assert.NotNull(domainEvent);
            Assert.Equal(_betId, ((BetCreated)domainEvent).BetId.Value);
            Assert.Equal(_creatorId, ((BetCreated)domainEvent).CreatorId.Value);
        }
        public async Task ShouldAddSubscription()
        {
            var subscriptionId    = Guid.NewGuid();
            var memberId          = Guid.NewGuid();
            var memberToSubscribe = new Member(new(subscriptionId), "member2", 300);
            var member            = new Member(new(memberId), "member1", 300);
            var domainListener    = new DomainEventsAccessor();
            var memberReposiory   = new InMemoryMemberRepository(new() { member, memberToSubscribe }, domainListener);
            var handler           = new SubscribeMemberCommanderHandler(memberReposiory, new InMemoryAuthenticationGateway(true, memberId));
            var command           = new SubscribeMemberCommand(subscriptionId);

            await handler.Handle(command, default);

            Assert.Collection(member.Subscriptions, x =>
            {
                Assert.Equal(subscriptionId, x.MemberId.Value);
            });
            Assert.Collection(domainListener.GetDomainEvents(), x =>
            {
                Assert.IsType <MemberSubscribed>(x);
                Assert.Equal(memberId, (x as MemberSubscribed).MemberId);
                Assert.Equal(subscriptionId, (x as MemberSubscribed).SubscriptionId);
            });
        }