模拟HttpContext。当前的测试初始化方法

我正在尝试添加单元测试到一个ASP。NET MVC应用程序。在我的单元测试中,我使用以下代码:

[TestMethod]
public void IndexAction_Should_Return_View() {
var controller = new MembershipController();
controller.SetFakeControllerContext("TestUser");


...
}

使用以下helper来模拟控制器上下文:

public static class FakeControllerContext {
public static HttpContextBase FakeHttpContext(string username) {
var context = new Mock<HttpContextBase>();


context.SetupGet(ctx => ctx.Request.IsAuthenticated).Returns(!string.IsNullOrEmpty(username));


if (!string.IsNullOrEmpty(username))
context.SetupGet(ctx => ctx.User.Identity).Returns(FakeIdentity.CreateIdentity(username));


return context.Object;
}


public static void SetFakeControllerContext(this Controller controller, string username = null) {
var httpContext = FakeHttpContext(username);
var context = new ControllerContext(new RequestContext(httpContext, new RouteData()), controller);
controller.ControllerContext = context;
}
}

这个测试类继承自一个基类,该基类具有以下特性:

[TestInitialize]
public void Init() {
...
}

在这个方法中,它调用一个库(我无法控制),试图运行以下代码:

HttpContext.Current.User.Identity.IsAuthenticated

现在你可能看到问题了。我已经针对控制器设置了伪HttpContext,但不是在这个基本的Init方法中。单元测试/模拟对我来说是非常新的,所以我想确保我做对了。我模拟出HttpContext的正确方法是什么,这样它就可以在我的控制器和Init方法中调用的任何库中共享。

173274 次浏览

HttpContext.Current返回一个System.Web.HttpContext的实例,它没有扩展System.Web.HttpContextBase。后来添加了HttpContextBase,以解决HttpContext难以模拟的问题。这两个类基本上是不相关的(HttpContextWrapper被用作它们之间的适配器)。

幸运的是,HttpContext本身是可伪造的,足以替换IPrincipal (User)和IIdentity

以下代码按预期运行,即使在控制台应用程序中也是如此:

HttpContext.Current = new HttpContext(
new HttpRequest("", "http://tempuri.org", ""),
new HttpResponse(new StringWriter())
);


// User is logged in
HttpContext.Current.User = new GenericPrincipal(
new GenericIdentity("username"),
new string[0]
);


// User is logged out
HttpContext.Current.User = new GenericPrincipal(
new GenericIdentity(String.Empty),
new string[0]
);

下面的Test Init也将完成这项工作。

[TestInitialize]
public void TestInit()
{
HttpContext.Current = new HttpContext(new HttpRequest(null, "http://tempuri.org", null), new HttpResponse(null));
YourControllerToBeTestedController = GetYourToBeTestedController();
}

如果你的应用程序由第三方在内部重定向,那么最好以以下方式模拟HttpContext:

HttpWorkerRequest initWorkerRequest = new SimpleWorkerRequest("","","","",new StringWriter(CultureInfo.InvariantCulture));
System.Web.HttpContext.Current = new HttpContext(initWorkerRequest);
System.Web.HttpContext.Current.Request.Browser = new HttpBrowserCapabilities();
System.Web.HttpContext.Current.Request.Browser.Capabilities = new Dictionary<string, string> { { "requiresPostRedirectionHandling", "false" } };

我知道这是一个较老的主题,但是为单元测试模拟MVC应用程序是我们经常做的事情。

我只是想添加我的经验模仿MVC 3应用程序使用Moq 4升级到Visual Studio 2013。没有一个单元测试在调试模式下工作,HttpContext在试图查看变量时显示“无法计算表达式”。

原来visual studio 2013有问题评估一些对象。为了让调试模拟的web应用程序再次工作,我不得不在工具=>选项=>调试=>常规设置中检查“使用托管兼容模式”。

我通常是这样做的:

public static class FakeHttpContext
{
public static void SetFakeContext(this Controller controller)
{


var httpContext = MakeFakeContext();
ControllerContext context =
new ControllerContext(
new RequestContext(httpContext,
new RouteData()), controller);
controller.ControllerContext = context;
}




private static HttpContextBase MakeFakeContext()
{
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>();
var user = new Mock<IPrincipal>();
var identity = new Mock<IIdentity>();


context.Setup(c=> c.Request).Returns(request.Object);
context.Setup(c=> c.Response).Returns(response.Object);
context.Setup(c=> c.Session).Returns(session.Object);
context.Setup(c=> c.Server).Returns(server.Object);
context.Setup(c=> c.User).Returns(user.Object);
user.Setup(c=> c.Identity).Returns(identity.Object);
identity.Setup(i => i.IsAuthenticated).Returns(true);
identity.Setup(i => i.Name).Returns("admin");


return context.Object;
}




}

然后像这样启动上下文

FakeHttpContext.SetFakeContext(moController);

直接调用控制器中的Method

long lReportStatusID = -1;
var result = moController.CancelReport(lReportStatusID);