kas*_*tan 10 asp.net-mvc unit-testing moq urlhelper
我使用NUnit和Moq库进行单元测试.我需要模拟重载的Url.Action(字符串,字符串,对象,字符串),因为我的控制器的动作使用它来获取Action的绝对URL.
我现在尝试(看看MockUrlAction测试):
[TestFixture]
public class ControllerTests
{
[Test]
public void MockUrlAction()
{
var controller = new TestController();
controller.Url = new UrlHelper(new RequestContext(MvcMoqHelpers.FakeHttpContext(), new RouteData()), GetRouteCollection());
// it works
Assert.AreEqual("/PathToAction", controller.Url.Action("TestAction"));
// but it doesn't work
Assert.AreEqual("http://example.com/PathToAction", controller.Url.Action("TestAction", null, null, "http"));
}
private RouteCollection GetRouteCollection()
{
BundleTable.MapPathMethod = MapBundleItemPath;
var routes = new RouteCollection();
RouteConfig.RegisterRoutes(routes);
var adminArea = new AdminAreaRegistration();
var adminAreaRegistrationContext = new AreaRegistrationContext(adminArea.AreaName, routes);
adminArea.RegisterArea(adminAreaRegistrationContext);
return routes;
}
}
public static class MvcMoqHelpers
{
public static HttpContextBase FakeHttpContext()
{
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>();
request.Setup(r => r.AppRelativeCurrentExecutionFilePath).Returns("/");
request.Setup(r => r.ApplicationPath).Returns("/");
response.Setup(s => s.ApplyAppPathModifier(It.IsAny<string>())).Returns<string>(s => s);
context.Setup(ctx => ctx.Request).Returns(request.Object);
context.Setup(ctx => ctx.Response).Returns(response.Object);
context.Setup(ctx => ctx.Session).Returns(session.Object);
context.Setup(ctx => ctx.Server).Returns(server.Object);
return context.Object;
}
}
Run Code Online (Sandbox Code Playgroud)
并在线
Assert.AreEqual("http://example.com/PathToAction", controller.Url.Action("TestAction", null, null, "http"));
Run Code Online (Sandbox Code Playgroud)
我得到一个例外
System.NullReferenceException : Object reference not set to an instance of an object.
at System.Web.Mvc.UrlHelper.GenerateUrl(String routeName, String actionName, String controllerName, String protocol, String hostName, String fragment, RouteValueDictionary routeValues, RouteCollection routeCollection, RequestContext requestContext, Boolean includeImplicitMvcValues)
at System.Web.Mvc.UrlHelper.Action(String actionName, String controllerName, Object routeValues, String protocol)
Run Code Online (Sandbox Code Playgroud)
这对我来说很奇怪controller.Url.Action("TestAction"),但工作正常,但controller.Url.Action("TestAction", null, null, "http")事实并非如此.
PS MvcMoqHelpers.FakeHttpContext()来自这里,也许它会有助于回答这个问题.
所以问题是:我怎样才能得到Url.Action(字符串,字符串,对象,字符串)?
谢谢.
Nen*_*nad 10
你必须设置Request.Url,你在教程中提供了那段代码:
public static HttpContextBase FakeHttpContext(string url)
{
HttpContextBase context = FakeHttpContext();
context.Request.SetupRequestUrl(url);
return context;
}
Run Code Online (Sandbox Code Playgroud)
原因是 - 在您的Url.Action重载中您没有提供主机名和协议,因此MVC尝试从Request.Url中提取这些值
if (!String.IsNullOrEmpty(protocol) || !String.IsNullOrEmpty(hostName))
{
Uri requestUrl = requestContext.HttpContext.Request.Url;
protocol = (!String.IsNullOrEmpty(protocol)) ? protocol : Uri.UriSchemeHttp;
hostName = (!String.IsNullOrEmpty(hostName)) ? hostName : requestUrl.Host;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7301 次 |
| 最近记录: |