1

正如主题所说,我不知道如何在我的 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是只读的。有任何想法吗?

4

1 回答 1

3

如果要创建实例,则不必创建实例null,只需执行以下操作:

HttpPostedFileBase imageFile = null;

它是一个抽象类确实意味着您不能创建它的实例,但是声明该类型的变量并将其设置为null.

于 2014-08-16T11:47:58.197 回答