如何在ASP.Net MVC中模拟控制器上的请求?


171

我在使用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:值不能为null。参数名称: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:在不可重写成员上的无效设置:Moq.Mock.ThrowIfCantOverride(表达式设置,MethodInfo methodInfo)上的x => x.Headers [“ X-Requested-With”]

同样,似乎我无法设置请求标头。如何在RhinoMocks或Moq中设置此值?


将Request.IsAjaxRequest替换为Request.IsAjaxRequest()
eu-ge-ne

模拟Request.Headers [“ X-Requested-With”]或Request [“ X-Requested-With”],而不是Request.IsAjaxRequest()。我更新了我的问题
eu-ge-ne

Answers:


212

使用最小起订量

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
我收到消息“无法从uage推断方法'ISetupGetter <T,TProperty> Moq.Mock <T> .SetupGet <Tpropert> ....的类型参数。尝试显式指定类型参数。我要设置什么类型'var request ='是什么
Nissan

刚刚更新了我的答案-不是Request.IsAjaxRequest,而是Request.IsAjaxRequest()。也更新您的问题
eu-ge-ne

仍会生成:异常System.ArgumentException:System.ArgumentException:在不可重写成员上的无效设置:Moq.Mock.ThrowIfCantOverride(Expression setup,MethodInfo methodInfo)上的x => x.IsAjaxRequest()
Nissan,2009年

问题是IsAjaxRequest()是静态扩展方法,不能被模拟-我已经更新了答案。
eu-ge-ne

应该是context.SetupGet(x => x.Request).Returns(request.Object); 您上面的代码在返回时仍缺少's'还会导致异常System.ArgumentException:System.ArgumentException:在不可覆盖成员上的无效设置:x => x.Headers [“ X-Requested-With”]在Moq .Mock.ThrowIfCantOverride(表达式设置,MethodInfo methodInfo)错误消息
Nissan

17

对于使用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);

13

这是使用RhinoMocks的有效解决方案。我基于Moq解决方案,该解决方案位于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);
}

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

看起来您正在寻找这个,

 var requestMock = new Mock<HttpRequestBase>();
 requestMock.SetupGet(rq => rq["Age"]).Returns("2001");

控制器中的用法:

 public ActionResult Index()
 {
        var age = Request["Age"]; //This will return 2001
 }

3

您需要模拟HttpContextBase并将其放入ControllerContext属性,如下所示:

controller.ControllerContext = 
new ControllerContext(mockedHttpContext, new RouteData(), controller);

以及ockedHttpContext需要被嘲笑的是什么?它需要的RequestContext对象在构造函数中需要一个HttpContextBase()对象,并且HttpContextBase()没有接受零参数的构造函数。
日产

我试过了:var模拟=新的MockRepository(); var嘲笑的httpContext =模拟.DynamicMock <HttpContextBase>(); var嘲笑的HttpRequest = mocks.DynamicMock <HttpRequestBase>(); SetupResult.For(mockedhttpContext.Request).Return(mockedHttpRequest); var controller = new HomeController(Repository,LoginInfoProvider); controller.ControllerContext =新的嘲笑的httpContext,新的RouteData(),控制器); var result = controller.Index()as ViewResult; 但是仍然会引发相同的异常。
日产

您的链接无效,但以下内容似乎有效_request.Setup(o => o.Form).Returns(new NameValueCollection());
Vdex

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")));
}

它同时使用了请求收集和标头,这就是为什么我们需要同时为标头和请求收集创建设置的原因。

这将使该请求不是ajax请求时返回false。要使其返回true,可以执行以下操作:

_httpContext.SetupGet(x => x.Request["X-Requested-With"]).Returns("XMLHttpRequest");

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
}

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来替代。

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.