public void TestUpload() { //设置参数 const string iconUploadPath = "/a/b"; const string cssPath = "/a/b/icon.css"; const string imagePath = "/a/b/c.jpg"; var css = Icon.CreateCss(imagePath); //设置上传文件操作 _mockUpload.UploadImage(iconUploadPath).Returns(ImageInfo.Create(imagePath, 0, 0, 0)); //添加图标 _manager.Upload(Guid.NewGuid(), iconUploadPath, cssPath); //验证 _mockUpload.Received().UploadImage(iconUploadPath); _mockIconRepository.Received().Add(Arg.Is <Icon>(icon => icon.Name == "c")); _mockFileManager.Received().FilePath = Sys.GetPhysicalPath(cssPath); _mockFileManager.Received().Append(Arg.Is(css)); _mockFileManager.Received().Save(); }
public async Task<GenericOperationResult<bool>> AddPost(PostDto postDto, string host) { //Validate model var result = new GenericOperationResult<bool>(); var postValidator = new PostValidator(); var res = postValidator.Validate(postDto); if (res.IsValid) { try { int userId = int.Parse(httpContext.HttpContext.User.FindFirstValue(ClaimTypes.NameIdentifier)); if (postDto.Image != null) { var path = await uploadFile.UploadImage(postDto.Image, host); if (path != null) { postDto.ImageUrl = path; } else { result.Messages.Add("Invalid image type"); result.Status = OperationResultStatusEnum.Exception; return result; } } Post createPost = MapPost(postDto); createPost.UserId = userId; unitOfWork.PostRepository.Create(createPost); if (unitOfWork.SaveChanges()) { result.Data = true; result.Messages.Add("your post has been added "); result.Status = OperationResultStatusEnum.Succeeded; return result; } else { result.Data = false; result.Messages.Add("Sorry your post couldn't be uploaded try again!!"); result.Status = OperationResultStatusEnum.Failed; return result; } } catch (NullReferenceException nullEx) { throw new BusinessException("Null Refrence exception", nullEx); } catch (IndexOutOfRangeException outOfRangeEx) { throw new BusinessException("Out of range exception exception", outOfRangeEx); } catch (SqlException sqlEx) { throw new BusinessException("Sql Exception exception", sqlEx); } catch (Exception e) { throw new BusinessException("error Occured ", e); } } result.Data = false; result.Messages = res.Errors.Select(e => e.ErrorMessage).ToList(); result.Status = OperationResultStatusEnum.Failed; return result; }