//[ValidateAntiForgeryToken] //[Authorize(Policy = "CanWriteUsersData")] public ActionResult Create(UsersViewModel usersViewModel) { try { //_cache.Remove("ErrorData"); //ViewBag.ErrorData = null; // 视图模型验证 if (!ModelState.IsValid) { return(View(usersViewModel)); } #region 除命令验证 ////添加命令验证 //RegisterusersCommand registerusersCommand = new RegisterusersCommand(usersViewModel.Name, usersViewModel.Email, usersViewModel.BirthDate, usersViewModel.Phone); ////如果命令无效,证明有错误 //if (!registerusersCommand.IsValid()) //{ // List<string> errorInfo = new List<string>(); // //获取到错误,请思考这个Result从哪里来的 // foreach (var error in registerusersCommand.ValidationResult.Errors) // { // errorInfo.Add(error.ErrorMessage); // } // //对错误进行记录,还需要抛给前台 // ViewBag.ErrorData = errorInfo; // return View(usersViewModel); //} #endregion // 执行添加方法 _usersAppService.Create(usersViewModel); //var errorData = _cache.Get("ErrorData"); //if (errorData == null) // 是否存在消息通知 if (!_notifications.HasNotifications()) { ViewBag.Sucesso = "Users Registered!"; } return(View(usersViewModel)); } catch (Exception e) { return(View(e.Message)); } }
public async Task Create_ValidUser_ShouldBeCreated() { UserRoleDTO user = new UserRoleDTO() { UserName = "******", Email = "*****@*****.**", Password = "******", RoleName = "Manager" }; mockHttpClient .Setup(e => e.PostAsync(It.IsAny <Uri>(), It.IsAny <StringContent>())) .Returns(Task.FromResult(new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent("{\"userName\": \"Jack.John\",\"email\": \"[email protected]\"}") })); // Act var createdUser = await _usersManagementAppService.Create(user); // Assert createdUser.UserName.ShouldBe(user.UserName); createdUser.Email.ShouldBe(user.Email); }