如何测试在ASP.NET MVC一个ajax submition? [英] How to test an ajax submition in ASP.NET MVC?
本文介绍了如何测试在ASP.NET MVC一个ajax submition?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
具体来说,我怎么可以通过静态方法Request.IsAjaxRequest()?
Specifically, how can I pass the static method Request.IsAjaxRequest()?
我得到的异常System.ArgumentNullException当我尝试测试以下code:
I get the exception 'System.ArgumentNullException' when I try to test the following code:
if (Request.IsAjaxRequest())
{
return Json(data);
}
return View(data2);
我使用起订量。感谢您的帮助。
I'm using Moq. Thanks for any help.
推荐答案
您需要嘲讽请求和Request.Headers与Request.IsAjaxRequest()工作:
You need mocking Request and Request.Headers to work with Request.IsAjaxRequest():
var request = new Mock<HttpRequestBase>();
request.SetupGet(x => x.Headers).Returns(new System.Net.WebHeaderCollection {
{"X-Requested-With", "XMLHttpRequest"}
});
var context = new Mock<HttpContextBase>();
context.SetupGet(x => x.Request).Returns(request.Object);
var controller = new YourController();
controller.ControllerContext = new ControllerContext(context.Object, new RouteData(), controller);
这篇关于如何测试在ASP.NET MVC一个ajax submition?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文