public async void UpdateVehicle_ReturnVehicleUpdated(int id) { // Arrange var mockRepo = new Mock <IVehicleCatalogRepository>(); mockRepo.Setup(repo => repo.GetVehicle(id)) .Returns(Task.FromResult <Vehicle>(new Vehicle() { Id = id, Value = 10000.00M, BrandId = 1, ModelId = 1, YearModel = 1998, Fuel = "Gasoline" })); mockRepo.Setup(repo => repo.SaveAll()) .Returns(Task.FromResult <bool>(true)); var mapper = _dependencyFixture.ServiceProvider.GetService <IMapper>(); var logger = Mock.Of <ILogger <VehiclesController> >(); var controller = new VehiclesController(mockRepo.Object, mapper, logger); VehicleForUpdate newVehicle = new VehicleForUpdate() { value = 18000.50M, brandId = 1, modelId = 1, yearModel = 1998, fuel = "Gasoline" }; // Act var result = await controller.Put(id, newVehicle); // Assert var okResult = Assert.IsType <CreatedAtRouteResult>(result); var returnValue = Assert.IsType <string>(okResult.Value); Assert.Equal("Vehicle updated.", returnValue); }
public async Task <IActionResult> Put(int id, VehicleForUpdate VehicleForUpdate) { var Vehicle = await _repository.GetVehicle(id); // If it's null then there isn't a vehicle with the param id if (Vehicle == null) { return(NotFound(new { Message = $"Vehicle with id {id} not found." })); } else { Vehicle.Value = VehicleForUpdate.value; Vehicle.YearModel = VehicleForUpdate.yearModel; Vehicle.Fuel = VehicleForUpdate.fuel; // Update brand id only if it has been passed if (VehicleForUpdate.brandId.HasValue) { Vehicle.BrandId = VehicleForUpdate.brandId.Value; } // Update model id only if it has been passed if (VehicleForUpdate.modelId.HasValue) { Vehicle.ModelId = VehicleForUpdate.modelId.Value; } // Save it on database if (await _repository.SaveAll()) { return(CreatedAtRoute(nameof(GetVehicle), new { id = Vehicle.Id }, "Vehicle updated.")); } else { string errorMsg = "Failed updating vehicle on server."; _logger.LogError(errorMsg); throw new Exception(errorMsg); } } }