public async Task Handle_Customer_CreatedNitification() { // Arrage // get mock ef data set and database context var mockCustomerDataSet = new Mock <DbSet <Customer> >(); var databaseInstance = new Mock <IDatabaseContext>(); databaseInstance.Setup(x => x.Customers).Returns(mockCustomerDataSet.Object); // mock create customer command handler var mediatorMock = new Mock <IMediator>(); var customerCreatedCommandHandler = new CreateCustomerHandler(mediatorMock.Object, databaseInstance.Object); var newCustomerId = Guid.NewGuid(); // Act var result = await customerCreatedCommandHandler.Handle(new CreateCustomerCommand() { }, CancellationToken.None); result.CustomerId = newCustomerId; // Assert mediatorMock.Verify(m => m.Publish(It.Is <CustomerCreated>(cc => cc.CustomerId == newCustomerId), It.IsAny <CancellationToken>()), Times.Once); }
public async Task WhenCreatingEntityFails_ThenShouldReturnFailure() { var handler = new CreateCustomerHandler(_repositoryMock.Object); var cmdResult = await handler.Handle(new CreateCustomer(new Customer()), default); Assert.Equal(CommandOutcome.InvalidCommand, cmdResult.Outcome); }
public void DadoUmClienteComCPFJaCadastrado_Invalid() { var command = new CreateCustomerCommand("Nome usuario", "12345678901"); var handler = new CreateCustomerHandler(new FakeRepositoryCustomer()); var result = handler.Handle(command).Result; Assert.AreEqual(false, result.Success); }
public void DadoUmClienteValido_Valid() { var command = new CreateCustomerCommand("Nome usuario", "12345678900"); var handler = new CreateCustomerHandler(new FakeRepositoryCustomer()); var result = handler.Handle(command).Result; Assert.AreEqual(0, command.Notifications.Count); Assert.AreEqual(true, result.Success); }
public async Task Handle_CreateCustomer_WithAllMandatoryFields_AndEmailDoB_Successfully() { //Arrange var sut = new CreateCustomerHandler(_customerRegistrationContext); var createCustomerRequest = Utility.GetCreateCustomerRequest(withEmailAndDoB: true); //Act await sut.Handle(createCustomerRequest, CancellationToken.None); //Assert _customerRegistrationContext.Customers.Count().Should().Be(1); }
public async Task WhenEntityIsCreated_ThenShouldReturnNewEntity() { var customer = new Customer(); _repositoryMock.Setup(x => x.AddAsync(It.IsAny <Customer>())) .ReturnsAsync(customer); var handler = new CreateCustomerHandler(_repositoryMock.Object); var cmdResult = await handler.Handle(new CreateCustomer(customer), default); Assert.Equal(customer.Id, cmdResult.Entities[0].Id); }
public async Task WhenEntityIsCreated_ThenShouldReturnSuccess() { var customer = new Customer(); _repositoryMock.Setup(x => x.AddAsync(It.IsAny <Customer>())) .ReturnsAsync(customer); var handler = new CreateCustomerHandler(_repositoryMock.Object); var cmdResult = await handler.Handle(new CreateCustomer(customer), default); Assert.Equal(CommandOutcome.Accepted, cmdResult.Outcome); }
public async Task <IActionResult> Create([FromBody] CreateCustomerCommand command) { try { var result = await _handler.Handle(command); if (command.Notifications.Any()) { return(BadRequest(result)); } return(Ok(result)); } catch (Exception e) { throw e; } }