Exemplo n.º 1
0
        public async Task HandleAsync_WhenUserCreatedIntegrationEventIsValid_ShouldBeCompletedTask()
        {
            // Arrange
            var mockLogger = new MockLogger <UserCreatedIntegrationEventHandler>();

            TestMock.AccountService.Setup(accountRepository => accountRepository.AccountExistsAsync(It.IsAny <UserId>())).ReturnsAsync(false).Verifiable();

            TestMock.AccountService.Setup(accountRepository => accountRepository.CreateAccountAsync(It.IsAny <UserId>()))
            .ReturnsAsync(new DomainValidationResult <IAccount>())
            .Verifiable();

            var handler = new UserCreatedIntegrationEventHandler(TestMock.CashierAppSettingsOptions.Object, TestMock.AccountService.Object, mockLogger.Object);

            var integrationEvent = new UserCreatedIntegrationEvent
            {
                UserId = new UserId(),
                Email  = new EmailDto
                {
                    Address = "*****@*****.**"
                },
                Country = EnumCountryIsoCode.CA,
                Ip      = "10.10.10.10"
            };

            // Act
            await handler.HandleAsync(integrationEvent);

            // Assert
            TestMock.AccountService.Verify(accountRepository => accountRepository.AccountExistsAsync(It.IsAny <UserId>()), Times.Once);
            TestMock.AccountService.Verify(accountRepository => accountRepository.CreateAccountAsync(It.IsAny <UserId>()), Times.Once);
        }
        public async Task HandleAsync_WhenUserCreatedIntegrationEventIsValid_ShouldBeCompletedTask()
        {
            // Arrange
            var mockLogger = new MockLogger <UserCreatedIntegrationEventHandler>();

            TestMock.UserService.Setup(userService => userService.UserExistsAsync(It.IsAny <UserId>())).ReturnsAsync(false).Verifiable();

            TestMock.UserService.Setup(userService => userService.CreateUserAsync(It.IsAny <UserId>(), It.IsAny <string>()))
            .ReturnsAsync(new DomainValidationResult <User>())
            .Verifiable();

            TestMock.UserService.Setup(userService => userService.SendEmailAsync(It.IsAny <UserId>(), It.IsAny <string>(), It.IsAny <object>()))
            .Returns(Task.CompletedTask)
            .Verifiable();

            var handler = new UserCreatedIntegrationEventHandler(TestMock.UserService.Object, mockLogger.Object, TestMock.SendgridOptions.Object);

            var integrationEvent = new UserCreatedIntegrationEvent
            {
                UserId = new UserId(),
                Email  = new EmailDto
                {
                    Address  = "*****@*****.**",
                    Verified = true
                },
                Country = EnumCountryIsoCode.CA,
                Ip      = "10.10.10.10"
            };

            // Act
            await handler.HandleAsync(integrationEvent);

            // Assert
            TestMock.UserService.Verify(userService => userService.UserExistsAsync(It.IsAny <UserId>()), Times.Once);
            TestMock.UserService.Verify(userService => userService.CreateUserAsync(It.IsAny <UserId>(), It.IsAny <string>()), Times.Once);
            TestMock.UserService.Verify(userService => userService.SendEmailAsync(It.IsAny <UserId>(), It.IsAny <string>(), It.IsAny <object>()), Times.Once);
            mockLogger.Verify(Times.Never());
        }