嘲讽一个HttpContext的Response.Output与起订量 [英] Mocking a HttpContext Response.Output with Moq

查看:360
本文介绍了嘲讽一个HttpContext的Response.Output与起订量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用在 Hanselman的博客发现MvcMockHelpers类,将在嘲笑的HttpContext。我们扩展了它多少有些增加,我们需要一些认证数据,并在大多数情况下这已经很大。

I've been using the MvcMockHelpers class found at Hanselman's blog for passing in a mocked HttpContext. We extended it somewhat to add some authentication data we needed and for the most part this has been great.

我们遇到的问题是,我们给到控制器的情况下已在HttpContext.Response.Output,这是造成一些例外抛出一个空值。我不知道该怎么添加到得到这个工作。

The issue we are having is that the context we are giving to the controller has a null value in the HttpContext.Response.Output, which is causing some exceptions to be thrown. I'm not sure what to add to get this working.

下面是现有FakeHttpConext()方法:

Here is the existing FakeHttpConext() method:

public static HttpContextBase FakeHttpContext()
{
    var context = new Mock<HttpContextBase>();
    var request = new Mock<HttpRequestBase>();
    var response = new Mock<HttpResponseBase>();
    var session = new Mock<HttpSessionStateBase>();
    var server = new Mock<HttpServerUtilityBase>();

    // Our identity additions ...
    var user = new Mock<IPrincipal>();
    OurIdentity identity = (OurIdentity)Thread.CurrentPrincipal.Identity;

    context.Expect(ctx => ctx.Request).Returns(request.Object);
    context.Expect(ctx => ctx.Response).Returns(response.Object);
    context.Expect(ctx => ctx.Session).Returns(session.Object);
    context.Expect(ctx => ctx.Server).Returns(server.Object);
    context.Expect(ctx => ctx.User).Returns(user.Object);
    context.Expect(ctx => ctx.User.Identity).Returns(identity);
    return context.Object;
}

下面是爆炸方法(它是MVC的Contrib项目XmlResult的一部分):

Here is the exploding method (which is part of the MVC Contrib project's XmlResult):

public override void ExecuteResult(ControllerContext context)
{
    if (_objectToSerialize != null)
    {
       var xs = new XmlSerializer(_objectToSerialize.GetType());
       context.HttpContext.Response.ContentType = "text/xml";
       xs.Serialize(context.HttpContext.Response.Output, _objectToSerialize);
    }
}

什么我需要在FakeHttpContext方法添加到prevent空异常时context.HttpContext.Response.Output引用?

What do I need to add in the FakeHttpContext method to prevent the null exception when the context.HttpContext.Response.Output is referenced?

澄清:我在寻找需求的解决方案,以起订量来进行,而不是犀牛我在这个问题提到标题起订量,但忽视了问题的身体。对不起,任何混乱。

Clarification: The solution I'm looking for needs to be done in Moq, not Rhino. I mentioned Moq in the question title but neglected that in question body. Sorry for any confusion.

分辨率
我加code以下两行的FakeHttpContext()方法:

Resolution I added the following two lines of code to the FakeHttpContext() method:

var writer = new StringWriter();
context.Expect(ctx => ctx.Response.Output).Returns(writer);

这prevents空例外。不知道它的一个很好的答案长远,但它的作品现在。

This prevents the null exception. Not sure if its a good answer long term, but it works for now.

推荐答案

在试验MvcContrib( HTTP ://www.$c$cplex.com/mvcContrib )文件MvcMockHelps展示它是如何做

In the Tests for MvcContrib (http://www.codeplex.com/mvcContrib) the file MvcMockHelps shows how it is done.

这篇关于嘲讽一个HttpContext的Response.Output与起订量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆