public async Task <IActionResult> CreateAsync(PlatformViewModel viewModel) { if (!ModelState.IsValid) { return(View("Create", viewModel)); } var dto = _mapper.Map <PlatformDto>(viewModel); try { await _platformServices.CreateAsync(dto); } catch (ValidationException <Platform> e) { ModelState.AddModelError(e.Key, e.Message); _logger.LogWarning(e.Message); return(View("Create", viewModel)); } _logger.LogDebug($"Create new platform with name {viewModel.Name}"); return(RedirectToAction(nameof(GetAllAsync))); }
public void CreateAsync_ReturnsView_WhenCatchException() { var viewModel = CreatePlatformViewModel(); const string key = nameof(viewModel.Name); var value = viewModel.Name; A.CallTo(() => _platformService.CreateAsync(A <PlatformDto> ._)) .Throws(new EntityExistsWithKeyValueException <Platform>(key, value)); var result = _platformController.CreateAsync(viewModel).Result; result.Should().BeViewResult(); }