在 Web 中进行测试驱动的开发,比较大的困难是模拟 HttpContext ,它太复杂了。
moq 框架提供了强大的模拟能力,但是,模拟一个 HttpContext 对象还是需要自己来动手。
为此,我自己写了一个方法来完成这个工作。其中,还顺便使用 Log4Net 来输出一下它的工作情况。
05 |
/// AppRelativeCurrentExecutionFilePath, |
09 |
/// ApplyAppPathModifier |
11 |
/// <returns></returns> |
12 |
private Moq.Mock<System.Web.HttpContextBase> CreateHttpContext() |
14 |
log4net.ILog log = log4net.LogManager.GetLogger( "CreateHttpContext" ); |
16 |
string ApplicationPath = "/" ; |
18 |
string AppRelativeCurrentExecutionFilePath = "~/" ; |
20 |
var contextMock = new Moq.Mock<System.Web.HttpContextBase>(); |
23 |
.Setup(c => c.Request.AppRelativeCurrentExecutionFilePath) |
24 |
.Returns(AppRelativeCurrentExecutionFilePath) |
25 |
.Callback(() => log.Info( "Calling AppRelativeCurrentExecutionFilePath" )); |
28 |
.Setup(c => c.Request.ApplicationPath) |
29 |
.Returns(ApplicationPath) |
30 |
.Callback(() => log.Info( "Calling ApplicationPath" )); |
31 |
contextMock.Setup(rc => rc.Request.PathInfo) |
33 |
.Callback(() => log.Info( "Calling PathInfo" )); |
36 |
.Setup(rc => rc.Response.ApplyAppPathModifier(Moq.It.IsAny< string >())) |
37 |
.Returns(( string s) => s) |
38 |
.Callback(( string s) => log.InfoFormat( "Calling ApplyAppPathModifier: {0}." , s)); |
虽然这个方法已经能够完成我需要的测试,但是,我希望能将它提炼一下,得到一个更加通用的 Mock 方法。
很快,我发现这个工作已经在很久以前被 Scott Hanselman 介绍过一次了,其中甚至还写了不同的 Mock 框架下的提供方法。不过 moq 版本的作者不是他,而是另外一个人 Daniel Cazzulino, 这篇文章你可以在这里找到:http://www.hanselman.com/blog/ASPNETMVCSessionAtMix08TDDAndMvcMockHelpers.aspx,还可以顺便看一下 Daniel Cazzulino 的博客:http://blogs.clariusconsulting.net/kzu/
不过,他的文章是在 2008 年写的,实在太古老了。当时还是 MVC 的 Preview2 ,moq 在那个时候就已经诞生了,看来我太孤陋寡闻了。
回到我们的主题,经过了这么长的时间,moq 已经有了一些变化,使用最新的 moq 语法修改之后,发现他的的代码没有能够通过测试。
与我的比较一下,发现其中少了对 Response 中 ApplyAppPathModifier 方法的支持,增加之后就正常了。
下面是修改之后的代码,希望能给你带来方便。
003 |
using System.Text.RegularExpressions; |
005 |
using System.Collections.Specialized; |
006 |
using System.Web.Mvc; |
007 |
using System.Web.Routing; |
013 |
public static class MvcMockHelpers |
015 |
public static HttpContextBase FakeHttpContext() |
017 |
var context = new Mock<HttpContextBase>(); |
018 |
var request = new Mock<HttpRequestBase>(); |
019 |
var response = new Mock<HttpResponseBase>(); |
020 |
var session = new Mock<HttpSessionStateBase>(); |
021 |
var server = new Mock<HttpServerUtilityBase>(); |
025 |
.Setup(rsp => rsp.ApplyAppPathModifier(Moq.It.IsAny< string >())) |
026 |
.Returns(( string s) => s); |
028 |
context.Setup(ctx => ctx.Request).Returns(request.Object); |
029 |
context.Setup(ctx => ctx.Response).Returns(response.Object); |
030 |
context.Setup(ctx => ctx.Session).Returns(session.Object); |
031 |
context.Setup(ctx => ctx.Server).Returns(server.Object); |
033 |
return context.Object; |
036 |
public static HttpContextBase FakeHttpContext( string url) |
038 |
HttpContextBase context = FakeHttpContext(); |
039 |
context.Request.SetupRequestUrl(url); |
044 |
public static void SetFakeControllerContext( this Controller controller) |
046 |
var httpContext = FakeHttpContext(); |
047 |
ControllerContext context = new ControllerContext( new RequestContext(httpContext, new RouteData()), controller); |
048 |
controller.ControllerContext = context; |
051 |
static string GetUrlFileName( string url) |
053 |
if (url.Contains( "?" )) |
054 |
return url.Substring(0, url.IndexOf( "?" )); |
059 |
static NameValueCollection GetQueryStringParameters( string url) |
061 |
if (url.Contains( "?" )) |
063 |
NameValueCollection parameters = new NameValueCollection(); |
065 |
string [] parts = url.Split( "?" .ToCharArray()); |
066 |
string [] keys = parts[1].Split( "&" .ToCharArray()); |
068 |
foreach ( string key in keys) |
070 |
string [] part = key.Split( "=" .ToCharArray()); |
071 |
parameters.Add(part[0], part[1]); |
083 |
public static void SetHttpMethodResult( this HttpRequestBase request, string httpMethod) |
086 |
.Setup(req => req.HttpMethod) |
087 |
.Returns(httpMethod); |
091 |
public static void SetupRequestUrl( this HttpRequestBase request, string url) |
093 |
log4net.ILog log = log4net.LogManager.GetLogger( "CreateHttpContext" ); |
096 |
throw new ArgumentNullException( "url" ); |
098 |
if (!url.StartsWith( "~/" )) |
099 |
throw new ArgumentException( "Sorry, we expect a virtual url starting with \"~/\"." ); |
101 |
var mock = Mock.Get(request); |
103 |
mock.Setup(req => req.QueryString) |
104 |
.Returns(GetQueryStringParameters(url)); |
105 |
mock.Setup(req => req.AppRelativeCurrentExecutionFilePath) |
106 |
.Returns(GetUrlFileName(url)) |
107 |
.Callback(() => log.Info( "Calling AppRelativeCurrentExecutionFilePath" )); |
110 |
mock.Setup(req => req.PathInfo) |
111 |
.Returns( string .Empty); |
FROM: http://www.cnblogs.com/haogj/archive/2011/07/24/2115575.html