AddAsync 方法總是回傳 null
我模擬資料的代碼:
var mockFileService = new Mock<IFileService>();
var bytes = Encoding.UTF8.GetBytes("This is a dummy file");
IFormFile file = new FormFile(new MemoryStream(bytes), 0, bytes.Length, "Data", "dummy.pdf");
mockFileService.Setup(f => f.AddAsync(file, "pathToFolder", "nameFile")).ReturnsAsync("pathToFile");
_fileService = mockFileService.Object;
我模擬的界面:
public interface IFileService
{
Task<string> AddAsync(IFormFile file, string pathToFolder, string nameFile);
Task<string> AddAsync(byte[] file, string pathToFolder, string nameFile);
Task AddImagesJpegAsync(Image[] images, string path, string imagesPrefix);
Task<byte[]> GetAsync(string pathToFile);
void DeleteFolder(string path);
void CreateFolder(string path);
}
uj5u.com熱心網友回復:
在您的模擬設定中,您是這樣說的:
mockFileService.Setup(f => f.AddAsync(
file, "pathToFolder", "nameFile"))....
這就是說,只要AddAsync使用這些精確的引數值在您的模擬上呼叫該方法,并且對于物件參考,它也必須是您剛剛創建的同一個物件。相反,您應該使用It.AsAnyMoq 提供的方法,例如:
mockFileService.Setup(f => f.AddAsync(
It.AsAny<IFormFile>(), It.AsAny<string>(), It.AsAny<string>()))....
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/440801.html
