asp.net-mvc – 单元测试Url.Action(使用Rhino Mocks?)

前端之家收集整理的这篇文章主要介绍了asp.net-mvc – 单元测试Url.Action(使用Rhino Mocks?)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我试图为UrlHelper扩展方法编写一个测试,这个方法是这样使用的:
  1. Url.Action<TestController>(x => x.TestAction());

但是,我无法正确设置,以便我可以创建一个新的UrlHelper,然后断言返回的URL是预期的.这是我所拥有的,但我对任何不涉及嘲笑的东西都是开放的. ; O)

  1. [Test]
  2. public void Should_return_Test_slash_TestAction()
  3. {
  4. // Arrange
  5. RouteTable.Routes.Add("TestRoute",new Route("{controller}/{action}",new MvcRouteHandler()));
  6. var mocks = new MockRepository();
  7. var context = mocks.FakeHttpContext(); // the extension from hanselman
  8. var helper = new UrlHelper(new RequestContext(context,new RouteData()),RouteTable.Routes);
  9.  
  10. // Act
  11. var result = helper.Action<TestController>(x => x.TestAction());
  12.  
  13. // Assert
  14. Assert.That(result,Is.EqualTo("Test/TestAction"));
  15. }

我尝试将其更改为urlHelper.Action(“Test”,“TestAction”),但它会失败,所以我知道这不是我的extensionmethod不工作. NUnit返回:

  1. NUnit.Framework.AssertionException: Expected string length 15 but was 0. Strings differ at index 0.
  2. Expected: "Test/TestAction"
  3. But was: <string.Empty>

我已经验证路由是注册和工作,我正在使用Hanselmans扩展来创建一个假HttpContext.以下是我的UrlHelper扩展方法

  1. public static string Action<TController>(this UrlHelper urlHelper,Expression<Func<TController,object>> actionExpression) where TController : Controller
  2. {
  3. var controllerName = typeof(TController).GetControllerName();
  4. var actionName = actionExpression.GetActionName();
  5.  
  6. return urlHelper.Action(actionName,controllerName);
  7. }
  8.  
  9. public static string GetControllerName(this Type controllerType)
  10. {
  11. return controllerType.Name.Replace("Controller",string.Empty);
  12. }
  13.  
  14. public static string GetActionName(this LambdaExpression actionExpression)
  15. {
  16. return ((MethodCallExpression)actionExpression.Body).Method.Name;
  17. }

任何想法,我失踪了,让它工作?
/克里斯托弗

解决方法

它不工作的原因是RouteCollection对象在内部调用HttpResponseBase上的ApplyAppPathModifier方法.看起来Hanselman的模拟代码没有对该方法设置任何期望,所以它返回null,这就是为什么你所有的调用UrlHelper上的Action方法都返回一个空字符串的原因.该修复将是在HttpResponseBase mock的ApplyAppPathModifier方法上设置一个期望,以返回传递给它的值.我不是Rhino Mocks的专家,所以我不完全确定语法.如果您正在使用Moq,那么它将如下所示:
  1. httpResponse.Setup(r => r.ApplyAppPathModifier(It.IsAny<string>()))
  2. .Returns((string s) => s);

或者,如果你只是使用一个手工制作的模拟,这样的工作:

  1. internal class FakeHttpContext : HttpContextBase
  2. {
  3. private HttpRequestBase _request;
  4. private HttpResponseBase _response;
  5.  
  6. public FakeHttpContext()
  7. {
  8. _request = new FakeHttpRequest();
  9. _response = new FakeHttpResponse();
  10. }
  11.  
  12. public override HttpRequestBase Request
  13. {
  14. get { return _request; }
  15. }
  16.  
  17. public override HttpResponseBase Response
  18. {
  19. get { return _response; }
  20. }
  21. }
  22.  
  23. internal class FakeHttpResponse : HttpResponseBase
  24. {
  25. public override string ApplyAppPathModifier(string virtualPath)
  26. {
  27. return virtualPath;
  28. }
  29. }
  30.  
  31. internal class FakeHttpRequest : HttpRequestBase
  32. {
  33. private NameValueCollection _serverVariables = new NameValueCollection();
  34.  
  35. public override string ApplicationPath
  36. {
  37. get { return "/"; }
  38. }
  39.  
  40. public override NameValueCollection ServerVariables
  41. {
  42. get { return _serverVariables; }
  43. }
  44. }

上述代码应该是HttpContextBase的最小必要实现,以便对UrlHelper进行单元测试通过.我试过了,它的工作.希望这可以帮助.

猜你在找的asp.Net相关文章