Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/15.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# 如何在MVC单元测试中初始化请求对象?_C#_Asp.net Mvc - Fatal编程技术网

C# 如何在MVC单元测试中初始化请求对象?

C# 如何在MVC单元测试中初始化请求对象?,c#,asp.net-mvc,C#,Asp.net Mvc,我有以下控制器方法: public ActionResult Contact() { HttpPostedFileBase theFile = Reqeust.Files["myfile"]; ViewBag.Message = "Contact Page"; return View(); } 在单元测试中,我执行以下操作: HomeController controller = new HomeController(); controller.Contact(); 调用Cont

我有以下控制器方法:

public ActionResult Contact()
{
  HttpPostedFileBase theFile = Reqeust.Files["myfile"];
  ViewBag.Message = "Contact Page";
  return View();
}
在单元测试中,我执行以下操作:

HomeController controller = new HomeController();
controller.Contact();
调用Contact()时,请求将为null。我尝试了许多不同的方法来初始化请求对象,但都没有效果。我尝试了HttpContextWrapper(),但没有转换成HttpRequestBase。另外,controller.Request是只读的,所以我创建的任何HttpContext.Current都不能在那里工作


有什么想法吗?

在测试中实例化控制器时,您可以向其传递一个新的
ControllerContext
,其中包括一个
HttpContext
+mock
请求的模拟:

controller = new HomeController {
    ControllerContext = new ControllerContext { HttpContext = new MockHttpContext() }
};

或者,您可以使用类似或的框架。

在测试中实例化控制器时,您可以向其传递一个新的
ControllerContext
,其中包括一个
HttpContext
+mock
请求的模拟:

controller = new HomeController {
    ControllerContext = new ControllerContext { HttpContext = new MockHttpContext() }
};

或者,您可以使用类似或的框架。

您需要模拟它并设置ControllerContext。让我们假设您选择了这样的框架来简化事情。你的测试可能是这样的

// arrange
var mockHttpContext = new Mock<HttpContextBase>();
var response = new Mock<HttpResponseBase>();
mockHttpContext.SetupGet(x => x.Response).Returns(response.Object);

var controller = new HomeController()
{
    ControllerContext = new ControllerContext() 
    { 
        HttpContext = mockHttpContext.Object 
    }
};

// act    
controller.Contact();

// assert
//排列
var mockHttpContext=new Mock();
var response=newmock();
SetupGet(x=>x.Response).Returns(Response.Object);
var controller=新的HomeController()
{
ControllerContext=新的ControllerContext()
{ 
HttpContext=mockHttpContext.Object
}
};
//表演
controller.Contact();
//断言

当然,如果希望请求对象的
Files
属性返回某些内容,则需要为模拟设置适当的期望值。目前,您的控制器操作似乎对该文件没有任何用处。我猜在您的真实代码中,这个控制器操作实际上与上传的文件一起工作。因此,您将模拟HttpPostedFileBase,然后您可以断言已调用其上的某些方法,或者验证此操作的正确行为所需的任何方法。

您需要模拟它并设置ControllerContext。让我们假设您选择了这样的框架来简化事情。你的测试可能是这样的

// arrange
var mockHttpContext = new Mock<HttpContextBase>();
var response = new Mock<HttpResponseBase>();
mockHttpContext.SetupGet(x => x.Response).Returns(response.Object);

var controller = new HomeController()
{
    ControllerContext = new ControllerContext() 
    { 
        HttpContext = mockHttpContext.Object 
    }
};

// act    
controller.Contact();

// assert
//排列
var mockHttpContext=new Mock();
var response=newmock();
SetupGet(x=>x.Response).Returns(Response.Object);
var controller=新的HomeController()
{
ControllerContext=新的ControllerContext()
{ 
HttpContext=mockHttpContext.Object
}
};
//表演
controller.Contact();
//断言

当然,如果希望请求对象的
Files
属性返回某些内容,则需要为模拟设置适当的期望值。目前,您的控制器操作似乎对该文件没有任何用处。我猜在您的真实代码中,这个控制器操作实际上与上传的文件一起工作。因此,您将模拟
HttpPostedFileBase
,然后您可以断言它上的某些方法已被调用,或者您需要验证此操作的正确行为的任何方法。

我注意到您为响应编写了代码。我只是根据要求更改它,它也可以工作。你能详细说明一下这对于HttpPostedFileBase是如何工作的吗?我注意到你为响应编写了代码。我只是根据要求更改它,它也可以工作。你能详细说明一下这对HttpPostedFileBase的作用吗?