使用最小起订量为单元测试模拟的HttpContext [英] Mock HttpContext using moq for unit test

查看:137
本文介绍了使用最小起订量为单元测试模拟的HttpContext的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要的HttpContext的单元测试的模拟。但我挣扎吧。

I need a mock of HttpContext for unit testing. But I'm struggling with it.

我在做这将有SessionIdManager改变的sessionId通过编程的方法。
而SessionIdManager需要的HttpContext不HttpContextBase。

I'm making a method that would change sessionId by programmatically with SessionIdManager. And SessionIdManager requires HttpContext not HttpContextBase.

但我找不到任何为例作的HttpContext的嘲弄。所有的例子在那里只有使HttpContextBase。

But I couldn't find any example to make a mock of HttpContext. All examples out there are only to make HttpContextBase.

我试过以下,但他们没有工作。

I tried below but they didn't work

HttpContext httpContext = Mock<HttpContext>();
HttpContext httpContext = (HttpContext)GetMockHttpContextBase();

public HttpContextBase GetMockHttpContextBase()
{
   var context = new Mock<HttpContextBase>();
   var request = new Mock<HttpRequestBase>();
   var response = new Mock<HttpResponseBase>();
   var session = new Mock<HttpSessionStateBase>();
   var application = new Mock<HttpApplication>();
   var httpContext = new Mock<HttpContext>();
   var server = new Mock<HttpServerUtilityBase>();
   var user = new Mock<IPrincipal>();
   var identity = new Mock<IIdentity>();
   var urlHelper = new Mock<UrlHelper>();
   var routes = new RouteCollection();
   var requestContext = new Mock<RequestContext>();

   requestContext.Setup(x => x.HttpContext).Returns(context.Object);
   context.Setup(ctx => ctx.Request).Returns(request.Object);
   context.Setup(ctx => ctx.Response).Returns(response.Object);
   context.Setup(ctx => ctx.Session).Returns(session.Object);
   application.Setup(x => x.Context).Returns(httpContext.Object);
   context.Setup(ctx => ctx.ApplicationInstance).Returns(application.Object);
   context.Setup(ctx => ctx.Server).Returns(server.Object);
   context.Setup(ctx => ctx.User).Returns(user.Object);
   user.Setup(ctx => ctx.Identity).Returns(identity.Object);
   identity.Setup(id => id.IsAuthenticated).Returns(true);
   identity.Setup(id => id.Name).Returns("test");
   request.Setup(req => req.Url).Returns(new Uri("http://tempuri.org"));
   request.Setup(req => req.RequestContext).Returns(requestContext.Object);
   requestContext.Setup(x => x.RouteData).Returns(new RouteData());
   request.SetupGet(req => req.Headers).Returns(new NameValueCollection());

   return context.Object;
}

有没有什么办法让HttpContext的或使用HttpContextBase的模拟的HttpContext的?

Is there any way to make mock of HttpContext or to use HttpContextBase for HttpContext?

请帮助我的人。

推荐答案

这是这里常见的问题和问题的根源是别拿类型你没有自己。而不是复杂的嘲笑,试图重现一类你没有写的行为(这意味着你猜测它做什么,你需要什么模拟),你的code和外部对象之间引入一个抽象HttpContext的和模拟的抽象。作为模拟对象的先驱之一说道:

This is a common question here and the root of the problem is DON'T MOCK TYPES YOU DON'T OWN. Rather than complicated mocks to try and reproduce the behavior of a class you didn't write (which means you're guessing about what it does and what you need to mock), introduce an abstraction between your code and the external object HttpContext and mock that abstraction. As one of the pioneers of mock objects says:

对我来说,关键是当乔Walnes想出了激进的概念
  不要嘲笑你没有自己的类型。这意味着:通过停止跳跃
  箍封闭世界的图书馆工作,用测试来发现
  你的对象,他们彼此说些什么。

The key for me was when Joe Walnes came up with the radical notion of "Don't mock types you don't own". This means: stop jumping through hoops to work with closed-world libraries, use the tests to discover your objects and what they say to each other.

http://higherorderlogic.com/2004/02/the - 大 - 的想法 - 是 - 消息/

这篇关于使用最小起订量为单元测试模拟的HttpContext的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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