C# 如何对请求进行单元测试。表单[“测试”]?

C# 如何对请求进行单元测试。表单[“测试”]?,c#,forms,unit-testing,request,moq,C#,Forms,Unit Testing,Request,Moq,以下是我的控制器的方法:- [HttpPost] public ActionResult Search(SearchViewModel model) { string selection = Request.Form["Options"]; if (selection == "str1") { ----------------------------- } } 它是基于从Req

以下是我的控制器的方法:-

   [HttpPost]
   public ActionResult Search(SearchViewModel model)
   {
       string selection = Request.Form["Options"];
       if (selection == "str1")
       {
           -----------------------------
       }        
   }

它是基于从Request.Form.But Request.Form中获取值的条件,Request.Form只是提供Get属性,我不能在单元测试方法中设置它的值。有没有办法设置它的值?

你是指Request.Form[“Options”]的值?如果是这样,您应该知道这是一个HttpRequest,您需要做的是从http请求中模拟它,而不是从代码中模拟它,因为您不能在这里更改它。

不要在内部使用request.Form[“Options”]。您可以在SearchViewModel类中拥有Option属性,并可以使用它。
对于需要在控制器方法中使用会话的场景,您可以使用ModelBinder,我们可以模拟和单元测试请求。使用moq形成对象。请参考下面的代码

var controllercontext = new Mock<ControllerContext>();
        controllercontext.Setup(frm => frm.HttpContext.Request.Form.Set("Options", "1"));

Yourcontroller.ControllerContext = controllercontext.Object;
var response = YourController.Search(new SearchViewModel() {//Your Model Data..}) as ActionResult;
var controllercontext=new Mock();
controllercontext.Setup(frm=>frm.HttpContext.Request.Form.Set(“选项”,“1”));
Yourcontroller.ControllerContext=ControllerContext.Object;
var response=YourController.Search(新的SearchViewModel(){//您的模型数据..})作为ActionResult;

下面是一种模拟表单集合的工作方式
注意,我已经在上面maruthi代码的帮助下完成了Request.Form实例化。但是,分配表单字段与他的代码不兼容,因此您需要从安装程序返回它

 NameValueCollection form = new NameValueCollection();
 form["Key"] = "Value";    
 var controllercontext = new Mock<ControllerContext>();
 controllercontext.Setup(frm => frm.HttpContext.Request.Form).Returns(form);
 _controller.ControllerContext = controllercontext.Object;
NameValueCollection表单=新的NameValueCollection();
表格[“键”]=“值”;
var controllercontext=new Mock();
controllercontext.Setup(frm=>frm.HttpContext.Request.Form).Returns(Form);
_controller.ControllerContext=ControllerContext.Object;

以上方法都不适合我。我最终使用了以下没有Mock的代码

DefaultHttpContext httpContext = new DefaultHttpContext();
httpContext.Request.Scheme = "http";
httpContext.Request.Host = new HostString("localhost");
var formCol = new FormCollection(new Dictionary<string, 
Microsoft.Extensions.Primitives.StringValues>
{   
            { "key1", "value1" },
            { "key2", "value2" }
}); 
httpContext.Request.ContentType = "application/x-www-form-urlencoded";
httpContext.Request.Form = formCol;
var context = new MyContext();
var controller = new MyController(context);
controller.ControllerContext = new ControllerContext {
            HttpContext = httpContext
};
DefaultHttpContext httpContext=新的DefaultHttpContext();
httpContext.Request.Scheme=“http”;
httpContext.Request.Host=新主机字符串(“localhost”);
var formCol=new FormCollection(新字典
{   
{“key1”,“value1”},
{“key2”,“value2”}
}); 
httpContext.Request.ContentType=“应用程序/x-www-form-urlencoded”;
httpContext.Request.Form=formCol;
var context=new MyContext();
var controller=新的MyController(上下文);
controller.ControllerContext=新的ControllerContext{
HttpContext=HttpContext
};

你能描述一下模拟它的例子吗?我没有模拟http请求的例子。。但我说的是它自己的想法。快速搜索可在stackoverflow上显示此问题。。检查一下,然后从那里开始