Testing the ASP.Net MVC Ajax Module

We just started using ASP.Net MVC Release Candidate, and the test project we tested previously tested Ajax requests with MVC beta.

The old code looked something like this:

Mock<HttpRequestBase> request = new Mock<HttpRequestBase>();
Mock<HttpResponseBase> response = new Mock<HttpResponseBase>();
Mock<HttpContextBase> context = new Mock<HttpContextBase>();

context.Expect(c => c.Request).Returns(request.Object);
context.Expect(c => c.Response).Returns(response.Object);

request.Expect(req => req["__MVCASYNCPOST"]).Returns("true");

MyController controller = new MyController();
controller.ControllerContext = new ControllerContext(context.Object, new RouteData(), controller);
ViewResult result = controller.UpdateStatus() as ViewResult;

Then the call to UpdateStatus will use the IsMvcAjaxRequest () method for the request object to determine what needs to be returned to the browser.

A change in the candidate candidate ASP.Net Release MVC to Request.IsMvcAjaxRequest () for the extension method Request.IsAjaxRequest () means that the way we mock request headers changes to:

request.Expect(req => req["X-Requested-With"]).Returns("XMLHttpRequest");

I hope others find this useful.

+3
source share
2 answers

RC1. AJAX. , , , UpdateModel FormCollection. , Request.Form NameValueCollection. , , , , .

. MVC, ASP.NET MVC Ajax, Javascript AJAX. javascript __MVCASYNCPOST X-Requested-With HTTP. C:\Program Files\Microsoft ASP.NET\ASP.NET MVC RC\Temp\MvcWebApplicationProjectTemplateRC.cs.zip - Scripts.

+1

, , , , Ajax. , .

+1

Source: https://habr.com/ru/post/1702374/


All Articles