We have just started using ASP.Net MVC Release Candidate and the test project we have was previously testing Ajax requests with MVC beta.
我们刚刚开始使用ASP.Net MVC Release Candidate,我们以前的测试项目以MVC beta测试Ajax请求。
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;
The call to UpdateStatus would then use the IsMvcAjaxRequest() method on the request object to determine what to return to the browser.
然后,对UpdateStatus的调用将使用请求对象上的IsMvcAjaxRequest()方法来确定返回浏览器的内容。
The change in ASP.Net MVC Release Candidate to the Request.IsMvcAjaxRequest() to an extension method of Request.IsAjaxRequest() means that the way we mock the request headers is changed to:
将ASP.Net MVC Release Candidate更改为Request.IsMvcAjaxRequest()到Request.IsAjaxRequest()的扩展方法意味着我们模拟请求标头的方式更改为:
request.Expect(req => req["X-Requested-With"]).Returns("XMLHttpRequest");
I hope others find this useful
我希望其他人觉得这很有用
2 个解决方案
#1
I was aware that Scott Gu's blog mentions the change but it doesn't provide any code examples of the impact on test code or the value that needs to be returned in order to mock an Ajax request. thought this might provide a quick and simple fix for this change in the release candidate.
我知道Scott Gu的博客提到了这一变化,但它没有提供任何代码示例,说明对模拟Ajax请求时对测试代码或需要返回的值的影响。认为这可能会为候选版本中的此更改提供快速而简单的修复。
#2
This issue and many others are covered in Scott Guthrie's blog on RC1. Look for the section on AJAX improvements. Another thing that I have noticed is that UpdateModel no longer takes a FormCollection. I'm having to rework my unit tests to mock up the Request.Form NameValueCollection. The resulting code is probably better, though, so the pain is worth it.
Scott Guthrie在RC1上的博客中介绍了这个问题和许多其他问题。查看有关AJAX改进的部分。我注意到的另一件事是UpdateModel不再采用FormCollection。我不得不重做我的单元测试来模拟Request.Form NameValueCollection。但结果代码可能更好,所以痛苦是值得的。
EDIT: Another gotcha. If you have an existing MVC application that uses ASP.NET MVC Ajax, you'll need to manually update your Javascript files or it won't recognize the requests as AJAX requests. The old javascript files add the __MVCASYNCPOST
form field mechanism instead of setting the X-Requested-With
HTTP header. I found the new versions in C:\Program Files\Microsoft ASP.NET\ASP.NET MVC RC\Temp\MvcWebApplicationProjectTemplateRC.cs.zip
-- in the Scripts directory.
编辑:另一个问题。如果您有一个使用ASP.NET MVC Ajax的现有MVC应用程序,则需要手动更新Javascript文件,否则它将无法将请求识别为AJAX请求。旧的javascript文件添加__MVCASYNCPOST表单字段机制,而不是设置X-Requested-With HTTP标头。我在Ccripts目录中的C:\ Program Files \ Microsoft ASP.NET \ ASP.NET MVC RC \ Temp \ MvcWebApplicationProjectTemplateRC.cs.zip中找到了新版本。
#1
I was aware that Scott Gu's blog mentions the change but it doesn't provide any code examples of the impact on test code or the value that needs to be returned in order to mock an Ajax request. thought this might provide a quick and simple fix for this change in the release candidate.
我知道Scott Gu的博客提到了这一变化,但它没有提供任何代码示例,说明对模拟Ajax请求时对测试代码或需要返回的值的影响。认为这可能会为候选版本中的此更改提供快速而简单的修复。
#2
This issue and many others are covered in Scott Guthrie's blog on RC1. Look for the section on AJAX improvements. Another thing that I have noticed is that UpdateModel no longer takes a FormCollection. I'm having to rework my unit tests to mock up the Request.Form NameValueCollection. The resulting code is probably better, though, so the pain is worth it.
Scott Guthrie在RC1上的博客中介绍了这个问题和许多其他问题。查看有关AJAX改进的部分。我注意到的另一件事是UpdateModel不再采用FormCollection。我不得不重做我的单元测试来模拟Request.Form NameValueCollection。但结果代码可能更好,所以痛苦是值得的。
EDIT: Another gotcha. If you have an existing MVC application that uses ASP.NET MVC Ajax, you'll need to manually update your Javascript files or it won't recognize the requests as AJAX requests. The old javascript files add the __MVCASYNCPOST
form field mechanism instead of setting the X-Requested-With
HTTP header. I found the new versions in C:\Program Files\Microsoft ASP.NET\ASP.NET MVC RC\Temp\MvcWebApplicationProjectTemplateRC.cs.zip
-- in the Scripts directory.
编辑:另一个问题。如果您有一个使用ASP.NET MVC Ajax的现有MVC应用程序,则需要手动更新Javascript文件,否则它将无法将请求识别为AJAX请求。旧的javascript文件添加__MVCASYNCPOST表单字段机制,而不是设置X-Requested-With HTTP标头。我在Ccripts目录中的C:\ Program Files \ Microsoft ASP.NET \ ASP.NET MVC RC \ Temp \ MvcWebApplicationProjectTemplateRC.cs.zip中找到了新版本。