c# - UrlHelper 类在 Asp.net mvc 中抛出空异常

标签 c# asp.net-mvc unit-testing moq url-helper

我正在开发一个 Asp.net mvc 网络应用程序。我正在对我的应用程序进行单元测试。我正在使用 Moq 来模拟对象。我正在尝试模拟 Url 助手类。我找到了这个链接- asp.net mvc: how to mock Url.Content("~")? .使用该类,我可以在单元测试中模拟 UrlHelper 类。

我像这样创建了一个 BaseController

public class BaseController : Controller
{
    private IUrlHelper _urlHelper;

    public new IUrlHelper Url
    {
        get { return _urlHelper; }
        set { _urlHelper = value; }
    }
}

这里是IUrlHelper接口(interface)/抽象

public interface IUrlHelper
{
    string Action(string actionName);
    string Action(string actionName, object routeValues);
    string Action(string actionName, string controllerName);
    string Action(string actionName, RouteValueDictionary routeValues);
    string Action(string actionName, string controllerName, object routeValues);
    string Action(string actionName, string controllerName, RouteValueDictionary routeValues);
    string Action(string actionName, string controllerName, object routeValues, string protocol);
    string Action(string actionName, string controllerName, RouteValueDictionary routeValues, string protocol, string hostName);
    string Content(string contentPath);
    string Encode(string url);
    string RouteUrl(object routeValues);
    string RouteUrl(string routeName);
    string RouteUrl(RouteValueDictionary routeValues);
    string RouteUrl(string routeName, object routeValues);
    string RouteUrl(string routeName, RouteValueDictionary routeValues);
    string RouteUrl(string routeName, object routeValues, string protocol);
    string RouteUrl(string routeName, RouteValueDictionary routeValues, string protocol, string hostName);
}

我在单元测试中模拟了 Url.Content()

Mock<IUrlHelper> urlHelperMock = new Mock<IUrlHelper>();
urlHelperMock
    .Setup(x => x.Content(It.IsAny<string>()))
    .Returns<string>(x => "~/" + x);// Mock Content method of Url Helper

测试运行成功,运行良好。问题是我不能在实时应用程序中使用它。我的意思是当我运行我的应用程序并调用 Url.Content("path") 时,它会抛出 null 异常。

例如

public class ExampleController : BaseController
{
    public string GetPath()
    {
       return Url.Content("~/Path/To/File");
    }
}

Url.Content 在此代码中抛出 null 异常。它在单元测试中运行良好。我该如何修复该代码?

最佳答案

在这种情况下,确实不需要抽象 UrlHelper。只需为测试模拟助手以执行所需的操作。

例如,假设

public class ExampleController : Controller {
    public string GetContent() {
       return Url.Content("~/Path/To/File");
    }
}

这是一个模拟 UrlHelper 的测试,它使用 moq 就像在原始示例中一样工作

[TestClass]
public class UrlHelperTest {
    [TestMethod]
    public void MockUrlHelper() {
        //Arrange            
        var expectedPath = "~/Path/To/File";
        var expectedContent = "<p>Fake content here<p>";
        var urlHelperMock = new Mock<UrlHelper>();
        urlHelperMock
            .Setup(m => m.Content(expectedPath))
            .Returns(expectedContent)
            .Verifiable();

        var sut = new ExampleController();
        sut.Url = urlHelperMock.Object; //Set mock UrlHelper        

        //Act
        var actualContent = sut.GetContent();

        //Assert
        urlHelperMock.Verify();
        Assert.AreEqual(expectedContent, actualContent);
    }
}

现在可以执行测试,并且框架将在生产中填充必要的 Controller 属性,因为从技术上讲,从原始框架到自定义抽象没有任何变化。

关于c# - UrlHelper 类在 Asp.net mvc 中抛出空异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40563791/

相关文章:

c# - Xpath 错误有一个无效的 token

c# - 我可以获得内存映射文件的路径吗? (.NET 4.0)

c# - 使用 LINQ-to-SQL 返回已筛选子对象的对象

java - 在 AndroidAnnotations SharedPreferences 中模拟链式方法调用

unit-testing - F# Assert.AreEquals 因两个文字而失败

c# - 从 SqlServer 到 C#

c# - Autofac 使用开放通用类解析开放通用接口(interface)

asp.net - 在 MVC 3 中使用 DependencyResolver 进行 Controller 实例化时出错

asp.net-mvc - 为什么我不能将普通 View 与 ApiController 一起使用?

python - Django 测试, 'SQLCompiler' 对象没有属性 'col_count'