正如主题所说,我不知道如何在我的 MVC 测试项目中将模拟对象返回为 null。我是进行单元测试的新手。
我有一个动作:
[HttpPost]
public ActionResult Edit(ClubToAddVM clubToAddVm, HttpPostedFileBase imageFile)
{
if (ModelState.IsValid)
{
if (imageFile!=null)
{
clubToAddVm.ImageMimeType = imageFile.ContentType;
clubToAddVm.ImageData = new byte[imageFile.ContentLength];
imageFile.InputStream.Read(clubToAddVm.ImageData, 0, imageFile.ContentLength);
}
}
...
}
我希望在我的测试中将 imageFile 对象作为空值传递。不幸的是,我无法创建HttpPostedFileBase
抽象类的实例,我想尝试这样的事情:
var mockImageFile = new Mock<HttpPostedFileBase>();
但是我不知道如何使它为空,因为它
mockImageFile.Object
是只读的。有任何想法吗?