我需要一个 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?
请任何人帮助我.
这是一个常见的问题,问题的根源是不要模拟你不拥有的类型.与其使用复杂的模拟来尝试重现您未编写的类的行为(这意味着您在猜测它的作用以及您需要模拟什么),不如在您的代码和外部对象 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:
对我来说,关键是乔·沃尔恩斯提出了激进的概念不要模拟你不拥有的类型".这意味着:停止跳跃箍与封闭世界的图书馆合作,使用测试来发现你的对象以及它们之间的对话.
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-big-idea-is-messaging/
这篇关于使用 moq 模拟 HttpContext 进行单元测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!