public async Task <IActionResult> Post(ModelForNew model) { // map to dto var modelResult = _mapper.Map <Model.Model>(model); // Add new model _repository.Add <Model.Model>(modelResult); // Save it on database if (await _repository.SaveAll()) { return(CreatedAtRoute(nameof(GetModel), new { id = modelResult.Id }, "Model created.")); } else { string errorMsg = "Failed adding model on server."; _logger.LogError(errorMsg); throw new Exception(errorMsg); } }
public async void AddModel_ReturnModelCreated() { // 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 <ModelsController> >(); var controller = new ModelsController(mockRepo.Object, mapper, logger); ModelForNew newModel = new ModelForNew() { name = "Fake", brandId = 1 }; // Act var result = await controller.Post(newModel); // Assert var okResult = Assert.IsType <CreatedAtRouteResult>(result); var returnValue = Assert.IsType <string>(okResult.Value); Assert.Equal("Model created.", returnValue); }