设置在嘲笑性质HttpContextBase [英] Setting properties on a mocked HttpContextBase

查看:181
本文介绍了设置在嘲笑性质HttpContextBase的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的一个ASP.NET MVC应用程序,并正尝试编写针对控制器动作,其中一些操作上的HttpContext属性,如会议,Request.Cookies时,Response.Cookies等一些单元测试我有一些麻烦搞清楚如何排列,法,断言......我可以看到排列和断言......但我无法找出如何在嘲笑HttpContextBase当所有的性质法案它的属性只有干将。我不能的设置的对我的嘲笑任何情况下从我的控制器操作中...所以它似乎并不十分有用。我是相当新的嘲讽,所以我敢肯定有,我失去了一些东西,但它似乎合乎逻辑,我认为我应该能够创建一个模拟对象,我可以在测试控制器操作的情况下使用,我实际上的设置的属性值,再后来声称,他们仍然是我设置他们,或者类似的东西。我缺少什么?

 公共静态HttpContextBase GetMockHttpContext()
    {
        VAR requestCookies =新的模拟< HttpCookieCollection>();
        VAR要求=新的模拟< Htt的prequestBase>();
        request.Setup(R => r.Cookies).Returns(requestCookies.Object);
        request.Setup(R = GT; r.Url).Returns(新的URI(http://example.org));        VAR responseCookies =新的模拟< HttpCookieCollection>();
        VAR响应=新的模拟< Htt的presponseBase>();
        response.Setup(R => r.Cookies).Returns(responseCookies.Object);        VAR语境=新的模拟< HttpContextBase>();        context.Setup(CTX => ctx.Request).Returns(request.Object);
        context.Setup(CTX => ctx.Response).Returns(response.Object);
        context.Setup(CTX = GT; ctx.Session).Returns(新莫克&所述; HttpSessionStateBase方式>()对象);
        context.Setup(CTX = GT; ctx.Server).Returns(新莫克&所述;的HttpServerUtilityBase方式>()对象);
        context.Setup(CTX = GT; ctx.User).Returns(GetMockMembershipUser());
        context.Setup(CTX => ctx.User.Identity).Returns(context.Object.User.Identity);
        context.Setup(CTX = GT; ctx.Response.Output).Returns(新的StringWriter());        返回context.Object;
    }


解决方案

嘿,我觉得你刚刚经历有点脱节这里,没什么大不了的。 100%的可能,您描述的是。

我不是为什么你不能在你的嘲弄设置属性完全积极的,但是如果你发布全code你的测试,我很乐意去通过它你。刚刚送走我的头顶,我会建议两件事情:


  1. 有是设置()和SetupProperty()之间的差异。 SetupProperty()可能是你,如果你想在跟踪属性值,而不是从他们只是得到一个值后一度在做什么。


  2. 或者尝试调用SetupAllProperties()上的任何模拟,你需要设置一个属性。


检查起订量快速入门以及一些例子。

I'm working on an ASP.NET MVC application, and am trying to write some unit tests against controller actions, some of which manipulate properties on the HttpContext, such as Session, Request.Cookies, Response.Cookies, etc. I'm having some trouble figuring out how to "Arrange, Act, Assert"...I can see Arrange and Assert...but I'm having trouble figuring out how to "Act" on properties of a mocked HttpContextBase when all of its properties only have getters. I can't set anything on my mocked context from within my controller actions...so it doesn't seem very useful. I'm fairly new to mocking, so I'm sure there's something that I'm missing, but it seems logical to me that I should be able to create a mock object that I can use in the context of testing controller actions where I can actually set property values, and then later Assert that they're still what I set them to, or something like that. What am I missing?

    public static HttpContextBase GetMockHttpContext()
    {
        var requestCookies = new Mock<HttpCookieCollection>();
        var request = new Mock<HttpRequestBase>();
        request.Setup(r => r.Cookies).Returns(requestCookies.Object);
        request.Setup(r => r.Url).Returns(new Uri("http://example.org"));

        var responseCookies = new Mock<HttpCookieCollection>();
        var response = new Mock<HttpResponseBase>();
        response.Setup(r => r.Cookies).Returns(responseCookies.Object);

        var context = new Mock<HttpContextBase>();

        context.Setup(ctx => ctx.Request).Returns(request.Object);
        context.Setup(ctx => ctx.Response).Returns(response.Object);
        context.Setup(ctx => ctx.Session).Returns(new Mock<HttpSessionStateBase>().Object);
        context.Setup(ctx => ctx.Server).Returns(new Mock<HttpServerUtilityBase>().Object);
        context.Setup(ctx => ctx.User).Returns(GetMockMembershipUser());
        context.Setup(ctx => ctx.User.Identity).Returns(context.Object.User.Identity);
        context.Setup(ctx => ctx.Response.Output).Returns(new StringWriter());

        return context.Object;
    }

解决方案

Hey, I think you're just experiencing a bit of a disconnect here, no big deal. What you describe is 100% possible.

I'm not entirely positive on why you can't set properties on your Mocks, but if you post the full code for your test I'd be happy to go through it with you. Just off the top of my head, I'll suggest two things:

  1. There is a difference between Setup() and SetupProperty(). SetupProperty() is probably what you're after if you want to track values on properties, rather than just get a value from them once.

  2. Alternately try calling SetupAllProperties() on any mock that you need to set a property on.

Check the Moq quickstart as well for some examples.

这篇关于设置在嘲笑性质HttpContextBase的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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