I have a controller in C# using the ASP.Net MVC framework
我在c#中有一个使用ASP的控制器。净MVC框架
public class HomeController:Controller{
public ActionResult Index()
{
if (Request.IsAjaxRequest())
{
//do some ajaxy stuff
}
return View("Index");
}
}
I got some tips on mocking and was hoping to test the code with the following and RhinoMocks
我得到了一些关于mock的提示,希望用以下代码和RhinoMocks测试代码
var mocks = new MockRepository();
var mockedhttpContext = mocks.DynamicMock<HttpContextBase>();
var mockedHttpRequest = mocks.DynamicMock<HttpRequestBase>();
SetupResult.For(mockedhttpContext.Request).Return(mockedHttpRequest);
var controller = new HomeController();
controller.ControllerContext = new ControllerContext(mockedhttpContext, new RouteData(), controller);
var result = controller.Index() as ViewResult;
Assert.AreEqual("About", result.ViewName);
However I keep getting this error:
然而,我不断地得到这个错误:
Exception System.ArgumentNullException: System.ArgumentNullException : Value cannot be null. Parameter name: request at System.Web.Mvc.AjaxRequestExtensions.IsAjaxRequest(HttpRequestBase request)
系统异常。ArgumentNullException:系统。ArgumentNullException:值不能为空。参数名称:对System.Web.Mvc.AjaxRequestExtensions请求。IsAjaxRequest(HttpRequestBase请求)
Since the Request
object on the controller has no setter. I tried to get this test working properly by using recommended code from an answer below.
因为控制器上的请求对象没有setter。我试图通过使用下面的答案的推荐代码来让这个测试正常工作。
This used Moq instead of RhinoMocks, and in using Moq I use the following for the same test:
这个测试使用的是Moq而不是RhinoMocks,在使用Moq时,我使用了下面的测试:
var request = new Mock<HttpRequestBase>();
// Not working - IsAjaxRequest() is static extension method and cannot be mocked
// request.Setup(x => x.IsAjaxRequest()).Returns(true /* or false */);
// use this
request.SetupGet(x => x.Headers["X-Requested-With"]).Returns("XMLHttpRequest");
var context = new Mock<HttpContextBase>();
context.SetupGet(x => x.Request).Returns(request.Object);
var controller = new HomeController(Repository, LoginInfoProvider);
controller.ControllerContext = new ControllerContext(context.Object, new RouteData(), controller);
var result = controller.Index() as ViewResult;
Assert.AreEqual("About", result.ViewName);
but get the following error:
但有以下错误:
Exception System.ArgumentException: System.ArgumentException : Invalid setup on a non-overridable member: x => x.Headers["X-Requested-With"] at Moq.Mock.ThrowIfCantOverride(Expression setup, MethodInfo methodInfo)
系统异常。ArgumentException:系统。ArgumentException:不可重写成员的无效设置:x => x。在Moq.Mock标题(“X-Requested-With”)。ThrowIfCantOverride(表达式设置,MethodInfo MethodInfo)
Again, it seems like I cannot set the request header. How do I set this value, in RhinoMocks or Moq?
同样,似乎我无法设置请求头。如何在犀牛角或Moq中设置这个值?
7 个解决方案
#1
191
Using Moq:
使用Moq:
var request = new Mock<HttpRequestBase>();
// Not working - IsAjaxRequest() is static extension method and cannot be mocked
// request.Setup(x => x.IsAjaxRequest()).Returns(true /* or false */);
// use this
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);
UPDATED:
更新:
Mock Request.Headers["X-Requested-With"]
or Request["X-Requested-With"]
instead of Request.IsAjaxRequest()
.
模拟的要求。header [" x - requ斯特劳- with "]或Request[" x - requ斯特劳- with "]代替Request. isajaxrequest()。
#2
12
Here is a working solution using RhinoMocks. I've based it on a Moq solution I found at http://thegrayzone.co.uk/blog/2010/03/mocking-request-isajaxrequest/
这是一个使用犀牛角的有效解决方案。我基于我在http://thegrayzone.co.uk/blog/03/mocking-request-isajaxrequest/找到的Moq解决方案
public static void MakeAjaxRequest(this Controller controller)
{
MockRepository mocks = new MockRepository();
// Create mocks
var mockedhttpContext = mocks.DynamicMock<HttpContextBase>();
var mockedHttpRequest = mocks.DynamicMock<HttpRequestBase>();
// Set headers to pretend it's an Ajax request
SetupResult.For(mockedHttpRequest.Headers)
.Return(new WebHeaderCollection() {
{"X-Requested-With", "XMLHttpRequest"}
});
// Tell the mocked context to return the mocked request
SetupResult.For(mockedhttpContext.Request).Return(mockedHttpRequest);
mocks.ReplayAll();
// Set controllerContext
controller.ControllerContext = new ControllerContext(mockedhttpContext, new RouteData(), controller);
}
#3
12
For anyone using NSubstitute I was able to modify the above answers and do something like this... (where Details is the Action method name on the controller)
对于任何使用NSubstitute的人,我可以修改上面的答案,然后做如下的事情……(细节是控制器上的操作方法名称)
var fakeRequest = Substitute.For<HttpRequestBase>();
var fakeContext = Substitute.For<HttpContextBase>();
fakeRequest.Headers.Returns(new WebHeaderCollection { {"X-Requested-With", "XMLHttpRequest"}});
fakeContext.Request.Returns(fakeRequest);
controller.ControllerContext = new ControllerContext(fakeContext, new RouteData(), controller);
var model = new EntityTypeMaintenanceModel();
var result = controller.Details(model) as PartialViewResult;
Assert.IsNotNull(result);
Assert.AreEqual("EntityType", result.ViewName);
#4
5
Is AjaxRequest is an extension method. So you can do it the following way using Rhino:
是一个扩展方法。所以你可以用Rhino软件进行如下操作:
protected HttpContextBase BuildHttpContextStub(bool isAjaxRequest)
{
var httpRequestBase = MockRepository.GenerateStub<HttpRequestBase>();
if (isAjaxRequest)
{
httpRequestBase.Stub(r => r["X-Requested-With"]).Return("XMLHttpRequest");
}
var httpContextBase = MockRepository.GenerateStub<HttpContextBase>();
httpContextBase.Stub(c => c.Request).Return(httpRequestBase);
return httpContextBase;
}
// Build controller
....
controller.ControllerContext = new ControllerContext(BuildHttpContextStub(true), new RouteData(), controller);
#5
4
Looks like you are looking for this,
看起来你在找这个,
var requestMock = new Mock<HttpRequestBase>();
requestMock.SetupGet(rq => rq["Age"]).Returns("2001");
Usage in Controller :
使用控制器:
public ActionResult Index()
{
var age = Request["Age"]; //This will return 2001
}
#6
3
You need to mock HttpContextBase and put it into your ControllerContext property, like that:
您需要模拟HttpContextBase并将其放入ControllerContext属性,如下所示:
controller.ControllerContext =
new ControllerContext(mockedHttpContext, new RouteData(), controller);
#7
1
To make IsAjaxRequest()
to return false during Unit test you need to setup Request Headers as well as request collection value both in your test method as given below:
要使IsAjaxRequest()在单元测试期间返回false,您需要在测试方法中设置请求头和请求集合值,如下所示:
_request.SetupGet(x => x.Headers).Returns(new System.Net.WebHeaderCollection { { "X-Requested-With", "NotAjaxRequest" } });
_request.SetupGet(x=>x["X-Requested-With"]).Returns("NotAjaxRequest");
The reason for setting up both is hidden in implementation of IsAjaxRequest() which is given below:
在IsAjaxRequest()的实现中隐藏了设置两者的原因,如下所示:
public static bool IsAjaxRequest(this HttpRequestBase request)<br/>
{
if (request == null)
{
throw new ArgumentNullException("request");
}
return ((request["X-Requested-With"] == "XMLHttpRequest") || ((request.Headers != null) && (request.Headers["X-Requested-With"] == "XMLHttpRequest")));
}
It uses both request Collection and header this is why we need to create setup for both Header and Request Collection.
它同时使用请求集合和header这就是为什么我们需要为header和request集合创建setup。
this will make the request to return false when it is not a ajax request. to make it return true you can do the following:
这将使请求在非ajax请求时返回false。要使它返回为真,您可以做以下工作:
_httpContext.SetupGet(x => x.Request["X-Requested-With"]).Returns("XMLHttpRequest");
#1
191
Using Moq:
使用Moq:
var request = new Mock<HttpRequestBase>();
// Not working - IsAjaxRequest() is static extension method and cannot be mocked
// request.Setup(x => x.IsAjaxRequest()).Returns(true /* or false */);
// use this
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);
UPDATED:
更新:
Mock Request.Headers["X-Requested-With"]
or Request["X-Requested-With"]
instead of Request.IsAjaxRequest()
.
模拟的要求。header [" x - requ斯特劳- with "]或Request[" x - requ斯特劳- with "]代替Request. isajaxrequest()。
#2
12
Here is a working solution using RhinoMocks. I've based it on a Moq solution I found at http://thegrayzone.co.uk/blog/2010/03/mocking-request-isajaxrequest/
这是一个使用犀牛角的有效解决方案。我基于我在http://thegrayzone.co.uk/blog/03/mocking-request-isajaxrequest/找到的Moq解决方案
public static void MakeAjaxRequest(this Controller controller)
{
MockRepository mocks = new MockRepository();
// Create mocks
var mockedhttpContext = mocks.DynamicMock<HttpContextBase>();
var mockedHttpRequest = mocks.DynamicMock<HttpRequestBase>();
// Set headers to pretend it's an Ajax request
SetupResult.For(mockedHttpRequest.Headers)
.Return(new WebHeaderCollection() {
{"X-Requested-With", "XMLHttpRequest"}
});
// Tell the mocked context to return the mocked request
SetupResult.For(mockedhttpContext.Request).Return(mockedHttpRequest);
mocks.ReplayAll();
// Set controllerContext
controller.ControllerContext = new ControllerContext(mockedhttpContext, new RouteData(), controller);
}
#3
12
For anyone using NSubstitute I was able to modify the above answers and do something like this... (where Details is the Action method name on the controller)
对于任何使用NSubstitute的人,我可以修改上面的答案,然后做如下的事情……(细节是控制器上的操作方法名称)
var fakeRequest = Substitute.For<HttpRequestBase>();
var fakeContext = Substitute.For<HttpContextBase>();
fakeRequest.Headers.Returns(new WebHeaderCollection { {"X-Requested-With", "XMLHttpRequest"}});
fakeContext.Request.Returns(fakeRequest);
controller.ControllerContext = new ControllerContext(fakeContext, new RouteData(), controller);
var model = new EntityTypeMaintenanceModel();
var result = controller.Details(model) as PartialViewResult;
Assert.IsNotNull(result);
Assert.AreEqual("EntityType", result.ViewName);
#4
5
Is AjaxRequest is an extension method. So you can do it the following way using Rhino:
是一个扩展方法。所以你可以用Rhino软件进行如下操作:
protected HttpContextBase BuildHttpContextStub(bool isAjaxRequest)
{
var httpRequestBase = MockRepository.GenerateStub<HttpRequestBase>();
if (isAjaxRequest)
{
httpRequestBase.Stub(r => r["X-Requested-With"]).Return("XMLHttpRequest");
}
var httpContextBase = MockRepository.GenerateStub<HttpContextBase>();
httpContextBase.Stub(c => c.Request).Return(httpRequestBase);
return httpContextBase;
}
// Build controller
....
controller.ControllerContext = new ControllerContext(BuildHttpContextStub(true), new RouteData(), controller);
#5
4
Looks like you are looking for this,
看起来你在找这个,
var requestMock = new Mock<HttpRequestBase>();
requestMock.SetupGet(rq => rq["Age"]).Returns("2001");
Usage in Controller :
使用控制器:
public ActionResult Index()
{
var age = Request["Age"]; //This will return 2001
}
#6
3
You need to mock HttpContextBase and put it into your ControllerContext property, like that:
您需要模拟HttpContextBase并将其放入ControllerContext属性,如下所示:
controller.ControllerContext =
new ControllerContext(mockedHttpContext, new RouteData(), controller);
#7
1
To make IsAjaxRequest()
to return false during Unit test you need to setup Request Headers as well as request collection value both in your test method as given below:
要使IsAjaxRequest()在单元测试期间返回false,您需要在测试方法中设置请求头和请求集合值,如下所示:
_request.SetupGet(x => x.Headers).Returns(new System.Net.WebHeaderCollection { { "X-Requested-With", "NotAjaxRequest" } });
_request.SetupGet(x=>x["X-Requested-With"]).Returns("NotAjaxRequest");
The reason for setting up both is hidden in implementation of IsAjaxRequest() which is given below:
在IsAjaxRequest()的实现中隐藏了设置两者的原因,如下所示:
public static bool IsAjaxRequest(this HttpRequestBase request)<br/>
{
if (request == null)
{
throw new ArgumentNullException("request");
}
return ((request["X-Requested-With"] == "XMLHttpRequest") || ((request.Headers != null) && (request.Headers["X-Requested-With"] == "XMLHttpRequest")));
}
It uses both request Collection and header this is why we need to create setup for both Header and Request Collection.
它同时使用请求集合和header这就是为什么我们需要为header和request集合创建setup。
this will make the request to return false when it is not a ajax request. to make it return true you can do the following:
这将使请求在非ajax请求时返回false。要使它返回为真,您可以做以下工作:
_httpContext.SetupGet(x => x.Request["X-Requested-With"]).Returns("XMLHttpRequest");