Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/heroku/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何将模拟对象作为null返回_C#_Asp.net Mvc_Unit Testing_Moq_Httppostedfilebase - Fatal编程技术网

C# 如何将模拟对象作为null返回

C# 如何将模拟对象作为null返回,c#,asp.net-mvc,unit-testing,moq,httppostedfilebase,C#,Asp.net Mvc,Unit Testing,Moq,Httppostedfilebase,正如主题所说,我不知道如何在我的MVC测试项目中将模拟对象返回为null。我对做单元测试是新手 我有一个行动: [HttpPost] public ActionResult Edit(ClubToAddVM clubToAddVm, HttpPostedFileBase imageFile) { if (ModelState.IsValid) { if (imageFile!=null) { clubToAddVm.Ima

正如主题所说,我不知道如何在我的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对象作为null传递。不幸的是,我无法创建
HttpPostedFileBase
抽象类的实例,我想尝试以下方法:

var mockImageFile = new Mock<HttpPostedFileBase>();
var mockImageFile=new Mock();
但是我不知道怎么把它设为空,因为
mockImageFile.Object
为只读。
有什么想法吗

如果要将实例设为空,不必创建实例,只需执行以下操作:

HttpPostedFileBase imageFile = null;

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

完全可以,只需直接将对象设置为null,无需mock。我非常惭愧。很明显。我的思维被卡住了。谢谢你的帮助