我使用ASP.Net MVC框架在C#中有一个控制器
public class HomeController:Controller{
public ActionResult Index()
{
if (Request.IsAjaxRequest())
{
//do some ajaxy stuff
}
return View("Index");
}
}
我得到了一些关于模拟的提示,并希望用以下和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);
但是我一直收到这个错误:
异常 System.ArgumentNullException: System.ArgumentNullException:Value 不能为空。参数名称: 请求 System.Web.Mvc.AjaxRequestExtensions.IsAjaxRequest(HttpRequestBase 请求)
由于控制器上的Request
对象没有setter。我尝试使用以下答案中推荐的代码使测试正常运行。
这使用了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);
但收到以下错误:
异常System.ArgumentException: System.ArgumentException:无效 在不可覆盖的成员上设置:x =&GT; x.Headers [“X-Requested-With”]在Moq.Mock.ThrowIfCantOverride(Expression setup,MethodInfo methodInfo)
同样,我似乎无法设置请求标头。 如何在RhinoMocks或Moq中设置此值?
答案 0 :(得分:200)
使用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);
<强>更新:强>
模拟Request.Headers["X-Requested-With"]
或Request["X-Requested-With"]
而不是Request.IsAjaxRequest()
。
答案 1 :(得分:15)
对于使用NSubstitute的任何人,我能够修改上述答案并执行类似的操作......(其中Details是控制器上的Action方法名称)
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);
答案 2 :(得分:13)
这是一个使用RhinoMocks的工作解决方案。我的基础是我在http://thegrayzone.co.uk/blog/2010/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 :(得分:5)
AjaxRequest是一种扩展方法。因此,您可以使用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);
答案 4 :(得分:4)
您需要模拟HttpContextBase并将其放入您的ControllerContext属性中,如下所示:
controller.ControllerContext =
new ControllerContext(mockedHttpContext, new RouteData(), controller);
答案 5 :(得分:4)
看起来你正在寻找这个,
var requestMock = new Mock<HttpRequestBase>();
requestMock.SetupGet(rq => rq["Age"]).Returns("2001");
Controller中的用法:
public ActionResult Index()
{
var age = Request["Age"]; //This will return 2001
}
答案 6 :(得分:2)
要使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");
设置两者的原因隐藏在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")));
}
它同时使用请求Collection和header这就是我们需要为Header和Request Collection创建设置的原因。
这将使请求在不是ajax请求时返回false。要使其返回true,您可以执行以下操作:
_httpContext.SetupGet(x => x.Request["X-Requested-With"]).Returns("XMLHttpRequest");
答案 7 :(得分:0)
我发现了在Web API期间将HttpRequestMessage对象添加到您的请求的另一种方法,
[Test]
public void TestMethod()
{
var controllerContext = new HttpControllerContext();
var request = new HttpRequestMessage();
request.Headers.Add("TestHeader", "TestHeader");
controllerContext.Request = request;
_controller.ControllerContext = controllerContext;
var result = _controller.YourAPIMethod();
//Your assertion
}
答案 8 :(得分:0)
(聚会晚了一点,但我走了另一条路,以为我愿意分享)
要使用一种纯代码/模拟方式来测试它而不为Http类创建模拟,我实现了一个IControllerHelper,它具有一个Initialize方法,该方法以Request为参数,然后公开我想要的属性,例如:
public interface IControllerHelper
{
void Initialise(HttpRequest request);
string HostAddress { get; }
}
public class ControllerHelper : IControllerHelper
{
private HttpRequest _request;
public void Initialise(HttpRequest request)
{
_request = request;
}
public string HostAddress => _request.GetUri().GetLeftPart(UriPartial.Authority);
}
然后在我的控制器中,我在方法开始时调用初始化:
_controllerHelper.Initialise(Request);
然后我的代码仅依赖于可模拟的依赖关系。
return Created(new Uri($"{_controllerHelper.HostName}/api/MyEndpoint/{result.id}"), result);
对于功能测试,我只是覆盖合成中的iControllerHelper来替代它。
答案 9 :(得分:0)
在当前的 .NET (v 5) 中:
var controller = new SomeController(); // SomeController that inherits Microsoft.AspNetCore.Mvc.ControllerBase
var httpContext = new DefaultHttpContext(); // DefaultHttpContext class is part of Microsoft.AspNetCore.Http namespace
httpContext.Request.Headers.Add("origin", "0.0.0.1"); // Add your custom headers to request
controller.ControllerContext.HttpContext = httpContext;