如何在 ASP.Net MVC 中模拟控制器上的请求?
Posted
技术标签:
【中文标题】如何在 ASP.Net MVC 中模拟控制器上的请求?【英文标题】:How to mock the Request on Controller in ASP.Net MVC? 【发布时间】:2010-11-01 11:53:38 【问题描述】:我有一个使用 ASP.Net MVC 框架的 C# 控制器
public class HomeController:Controller
public ActionResult Index()
if (Request.IsAjaxRequest())
//do some ajaxy stuff
return View("Index");
我得到了一些关于 mocking 的提示,并希望使用以下和 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:值 不能为空。参数名称: 请求在 System.Web.Mvc.AjaxRequestExtensions.IsAjaxRequest(HttpRequestBase 请求)
由于控制器上的Request
对象没有设置器。我试图通过使用以下答案中的推荐代码来使该测试正常工作。
这使用了 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 => x.Headers["X-Requested-With"] 在 Moq.Mock.ThrowIfCantOverride(Expression 设置,方法信息方法信息)
再次,我似乎无法设置请求标头。 如何在 RhinoMocks 或 Moq 中设置此值?
【问题讨论】:
将 Request.IsAjaxRequest 替换为 Request.IsAjaxRequest() 模拟 Request.Headers["X-Requested-With"] 或 Request["X-Requested-With"] 而不是 Request.IsAjaxRequest()。我已经更新了我的问题 try this 【参考方案1】:使用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()
。
【讨论】:
我收到消息“方法 'ISetupGetter对于使用 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);
【讨论】:
【参考方案3】:这是一个使用 RhinoMocks 的有效解决方案。我基于在http://thegrayzone.co.uk/blog/2010/03/mocking-request-isajaxrequest/找到的最小起订量解决方案@
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);
【讨论】:
【参考方案4】:Is 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);
【讨论】:
【参考方案5】:看起来你正在寻找这个,
var requestMock = new Mock<HttpRequestBase>();
requestMock.SetupGet(rq => rq["Age"]).Returns("2001");
在控制器中的使用:
public ActionResult Index()
var age = Request["Age"]; //This will return 2001
【讨论】:
【参考方案6】:您需要模拟 HttpContextBase 并将其放入您的 ControllerContext 属性中,如下所示:
controller.ControllerContext =
new ControllerContext(mockedHttpContext, new RouteData(), controller);
【讨论】:
还有什么 mockedHttpContext 需要被模拟?它需要的 RequestContext 对象在构造函数中需要一个 HttpContextBase() 对象,而 HttpContextBase() 没有接受零参数的构造函数。 我试过了: var mocks = new MockRepository(); var mockedhttpContext = mocks.DynamicMock要使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")));
它同时使用请求集合和标头,这就是为什么我们需要为标头和请求集合创建设置。
这将使请求在不是 ajax 请求时返回 false。要使其返回 true,您可以执行以下操作:
_httpContext.SetupGet(x => x.Request["X-Requested-With"]).Returns("XMLHttpRequest");
【讨论】:
【参考方案8】:在当前的 .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;
【讨论】:
它也适用于Microsoft.AspNetCore.Mvc.Core
3.1【参考方案9】:
我找到了在 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
【讨论】:
【参考方案10】:(聚会有点晚了,但我走了一条不同的路,所以想分享一下)
为了在不为 Http 类创建模拟的情况下采用纯代码/模拟方式进行测试,我实现了一个 IControllerHelper,它有一个 Initialise 方法,该方法将请求作为参数,然后公开我想要的属性,例如:
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 即可。
【讨论】:
以上是关于如何在 ASP.Net MVC 中模拟控制器上的请求?的主要内容,如果未能解决你的问题,请参考以下文章
如何重定向到 $.AJAX 上的视图完成 - asp.net mvc 3