public async Task <IActionResult> Put(int id, BrandForUpdate brandForUpdate) { var brand = await _repository.GetBrand(id); // If it's null then there isn't a brand with the param id if (brand == null) { return(NotFound(new { Message = $"Brand with id {id} not found." })); } else { brand.Name = brandForUpdate.name; // Save it on database if (await _repository.SaveAll()) { return(CreatedAtRoute(nameof(GetBrand), new { id = brand.Id }, "Brand updated.")); } else { string errorMsg = "Failed updating brand on server"; _logger.LogError(errorMsg); throw new Exception(errorMsg); } } }
public async void UpdateBrand_ReturnBrandUpdated(int id) { // Arrange var mockRepo = new Mock <IVehicleCatalogRepository>(); mockRepo.Setup(repo => repo.GetBrand(id)) .Returns(Task.FromResult <Brand>(new Brand() { Id = id, Name = "Brand 1" })); 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); BrandForUpdate newBrand = new BrandForUpdate() { name = "Fake" }; // Act var result = await controller.Put(id, newBrand); // Assert var okResult = Assert.IsType <CreatedAtRouteResult>(result); var returnValue = Assert.IsType <string>(okResult.Value); Assert.Equal("Brand updated.", returnValue); }