public async Task <IActionResult> Post(BrandForNew brand) { // map to dto var brandResult = _mapper.Map <Brand>(brand); // Add new brand _repository.Add <Brand>(brandResult); // Save it on database if (await _repository.SaveAll()) { return(CreatedAtRoute(nameof(GetBrand), new { id = brandResult.Id }, "Brand created.")); } else { string errorMsg = "Failed adding brand on server."; _logger.LogError(errorMsg); throw new Exception(errorMsg); } }
public async void AddBrand_ReturnBrandCreated() { // Arrange var mockRepo = new Mock <IVehicleCatalogRepository>(); mockRepo.Setup(repo => repo.SaveAll()) .Returns(Task.FromResult <bool>(true)); var mapper = _dependencyFixture.ServiceProvider.GetService <IMapper>(); var logger = Mock.Of <ILogger <BrandsController> >(); var controller = new BrandsController(mockRepo.Object, mapper, logger); BrandForNew newBrand = new BrandForNew() { name = "Fake" }; // Act var result = await controller.Post(newBrand); // Assert var okResult = Assert.IsType <CreatedAtRouteResult>(result); var returnValue = Assert.IsType <string>(okResult.Value); Assert.Equal("Brand created.", returnValue); }