我正在使用
我有一个受保护的虚拟方法,通常用于上传字节到云服务。为了能够进行单元测试,我想要模拟这个方法,对于所有其他场景都可以正常工作。
问题出现在当我想让这个方法返回
我像这样模拟了我的图片提供程序。
我已经设置了我的模拟方法,就像这样:
调用上述模拟方法的方法如下所示:
.Net Core 2.0
作为我的项目框架,Moq v4.7.145
作为我的mocking框架。我有一个受保护的虚拟方法,通常用于上传字节到云服务。为了能够进行单元测试,我想要模拟这个方法,对于所有其他场景都可以正常工作。
问题出现在当我想让这个方法返回
null
时。这是为了模拟服务宕机或者实际上返回null
。我查看了一些关于SO的问题,但没有一个似乎有效。我像这样模拟了我的图片提供程序。
mockProvider
是云服务的模拟,而LoggerMock
是记录器的模拟。PictureProvider = new Mock<CloudinaryPictureProvider>(mockProvider.Object, LoggerMock.Object)
{
// CallBase true so it will only overwrite the method I want to be mocked.
CallBase = true
};
我已经设置了我的模拟方法,就像这样:
PictureProvider.Protected()
.Setup<Task<ReturnObject>>("UploadAsync", ItExpr.IsAny<ImageUploadParams>())
.Returns(null as Task<ReturnObject>);
我已尝试以各种方式转换返回对象,但到目前为止都没有成功。
我正在模拟的方法看起来像这样:
protected virtual async Task<ReturnObject> UploadAsync(ImageUploadParams uploadParams)
{
var result = await _cloudService.UploadAsync(uploadParams);
return result == null ? null : new ReturnObject
{
// Setting values from the result object
};
}
调用上述模拟方法的方法如下所示:
public async Task<ReturnObject> UploadImageAsync(byte[] imageBytes, string uploadFolder)
{
if (imageBytes == null)
{
// Exception thrown
}
var imageStream = new MemoryStream(imageBytes);
// It throws the NullReferenceException here.
var uploadResult = await UploadAsync(new ImageUploadParams
{
File = new FileDescription(Guid.NewGuid().ToString(), imageStream),
EagerAsync = true,
Folder = uploadFolder
});
if (uploadResult?.Error == null)
{
// This is basically the if statement I wanted to test.
return uploadResult;
}
{
// Exception thrown
}
}
每次在我的public UploadImageAsync
方法中调用受保护的(模拟的)方法时,都会抛出NullReferenceException
异常:
Message: Test method {MethodName} threw exception:
System.NullReferenceException: Object reference not set to an instance of an object.
我认为在模拟方法设置方面我漏掉了一些东西,只是我不知道是什么。如果我遗漏了任何内容,请告诉我!
ReturnsAsync
它会抛出一个nullreferenceexception吗? - user4189129.ReturnsAsync(null);
也应该可以工作,对吧? - Daniel Dušek