问题
I need a mock of HttpContext for unit testing. But I'm struggling with it.
I'm making a method that would change sessionId by programmatically with SessionIdManager. And SessionIdManager requires HttpContext not 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;
}
Is there any way to make mock of HttpContext or to use HttpContextBase for HttpContext?
Please help me anyone.
回答1:
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/
回答2:
If you're set on trying to mock HttpContext, then you're going to have to use quite a bit of boilerplate to essentially simulate it.
Phil Haack describes this approach in good detail in this blog post: http://haacked.com/archive/2005/06/11/simulating_httpcontext.aspx/
As I stated before, I wanted a “lightweight” means to test this method. There wouldn’t be a problem if HttpContext.Current was a valid instance of HttpContext. Luckily, the static Current property of HttpContext is both readable and writeable. All it takes is to set that property to a properly created instance of HttpContext. However, creating that instance wasn’t as straightforward as my first attempt. I’ll spare you the boring details and just show you what I ended up with.
He provides a simulator framework here: http://haacked.com/archive/2007/06/19/unit-tests-web-code-without-a-web-server-using-httpsimulator.aspx/
Keep in mind that you're likely venturing into integration testing with all of this, as the logic involved in simulating these objects is non-trivial and can interfere with the reliability of your tests.
来源:https://stackoverflow.com/questions/22604308/mock-httpcontext-using-moq-for-unit-test