Exemplo n.º 1
0
        public async Task Handle_GivenValidRequest_HandlerReturnsCorrectViewModel()
        {
            // Arrange
            var mediatorMock = new Mock <IMediator>();
            var sut          = new CreateCustomerCommand.Handler(_context, mediatorMock.Object);
            var newCustomer  = new Customer {
                FirstName = "Leonardo", MiddleName = "Da", LastName = "Vinci", Amount = new Decimal(145.2)
            };
            var newCustomerFullName   = $"{newCustomer.FirstName} {newCustomer.MiddleName} {newCustomer.LastName}";
            var amountInWordsExpected = newCustomer.Amount.ToWords(new System.Globalization.CultureInfo("en"));

            // Act
            var result = await sut.Handle(new CreateCustomerCommand { FirstName = newCustomer.FirstName, MiddleName = newCustomer.MiddleName, LastName = newCustomer.LastName, Amount = newCustomer.Amount.ToString() }, CancellationToken.None);


            // Assert
            mediatorMock
            .Setup(m => m.Send(It.IsAny <CreateCustomerCommand>(), It.IsAny <CancellationToken>()))
            .ReturnsAsync(result)
            .Verifiable();

            Assert.NotNull(result);
            Assert.Equal(result.FullName, newCustomerFullName);
            Assert.Equal(result.FormattedAmount, amountInWordsExpected);
        }
Exemplo n.º 2
0
        public void Handle_GivenValidRequest_ShouldRaiseCustomerCreationNotification()
        {
            var mediatorMock  = new Mock <IMediator>();
            var sut           = new CreateCustomerCommand.Handler(_context, mediatorMock.Object);
            var newCustomerId = "QAZQ1";

            var result = sut.Handle(new CreateCustomerCommand()
            {
                Id = newCustomerId
            }, CancellationToken.None);

            mediatorMock.Verify(
                m => m.Publish(It.Is <CustomerCreated>(cc => cc.CustomerId == newCustomerId),
                               It.IsAny <CancellationToken>()), Times.Once);
        }
Exemplo n.º 3
0
        public void Handle_GivenValidRequest_ShouldRaiseCustomerCreatedNotification()
        {
            // Arrange
            var mediatorMock  = new Mock <IMediator>();
            var sut           = new CreateCustomerCommand.Handler(context: _context, mediator: mediatorMock.Object);
            var newCustomerId = "QAZQ1";

            // Act
            var result = sut.Handle(new CreateCustomerCommand {
                Id = newCustomerId
            },
                                    cancellationToken: CancellationToken.None);


            // Assert
            mediatorMock.Verify(
                m => m.Publish(It.Is <CustomerCreated>(cc => cc.CustomerId == newCustomerId),
                               It.IsAny <CancellationToken>()), times: Times.Once);
        }
Exemplo n.º 4
0
        public void Handler_ShouldWork(string name, string driverLicense, string dob, string street, string city, string zipcode, string email, string phone)
        {
            var customerRepository = A.Fake <ICustomerRepository>();
            var uow = A.Fake <IUnitOfWork>();

            var command = new CreateCustomerCommand(name, driverLicense, DateTime.Parse(dob), street, city, zipcode, email, phone);
            var handler = new CreateCustomerCommand.Handler(customerRepository, uow, APITestsHelper.GetMapper());

            var result = handler.Handle(command, new CancellationToken()).Result;

            A.CallTo(() => customerRepository.Add(A <Customer> ._)).MustHaveHappenedOnceExactly();
            A.CallTo(() => uow.CommitAsync(A <CancellationToken> ._)).MustHaveHappenedOnceExactly();
            Assert.Equal(name, result.Name);
            Assert.Equal(driverLicense, result.DriverLicense);
            Assert.Equal(DateTime.Parse(dob), result.DOB);
            Assert.Equal(street, result.Street);
            Assert.Equal(city, result.City);
            Assert.Equal(zipcode, result.ZipCode);
            Assert.Equal(email, result.Email);
            Assert.Equal(phone, result.Phone);
        }