3

我想在我的控制器中测试一个操作,该操作使用 controllerContext 作为参数来生成基于第 3 部分库“Rotativa”的 pdf 文档。
这是动作(功能)的实现:

public ActionResult DetailsPrint(int? id)
{
    var a = new ViewAsPdf();
    a.ViewName = "../Ops/_2A1/Details";
    a.Model =UnitOfWork._2A1s.Get(id.Value);
    var pdfBytes = a.BuildPdf(ControllerContext);

    // return ActionResult
    MemoryStream ms = new MemoryStream(pdfBytes);
    return new FileStreamResult(ms, "application/pdf");

}

这就是我试图让单元测试工作的方式:

  • 构造函数

    public _2A1ControllerTest()
    {
        _mockRepository = new Mock<I2A1Repository>();
        var mockUoW = new Mock<IUnitOfWork>();
        _mockHttpContext = new Mock<HttpContextBase>();
        _mockRequest = new Mock<HttpRequestBase>();
        _mockDisplayModeContext = new Mock<IDisplayMode>();
        mockUoW.SetupGet(u => u._2A1s).Returns(_mockRepository.Object);
        _mockHttpContext.SetupGet(x => x.Request).Returns(_mockRequest.Object);
        _controller = new _2A1Controller(mockUoW.Object);
        _controller.MockCurrentUser("test.admin");
        _controller.ControllerContext = new ControllerContext(_mockHttpContext.Object, new System.Web.Routing.RouteData(), _controller);
    }
    
  • 测试功能

    [TestMethod]
    public void DetailsPrint_shouldPrint()
    {
        var result = _controller.DetailsPrint(1) as ActionResult;
        result.Should().BeOfType<ActionResult>();
    }
    

    当我执行测试时,出现以下错误: 在此处输入图像描述

测试名称:DetailsPrint_shouldPrint 测试全名:OPSReviewTest._2A1ControllerTest.DetailsPrint_shouldPrint 测试源:C:\inetpub\wwwroot\OpsReview\OPSReviewTest\Controllers\Api_2A1ControllerTest.cs:第 46 行测试结果:失败测试持续时间:0:04:39,3039007 结果 StackTrace :
在 System.Web.WebPages.DisplayModeProvider.GetDisplayMode(HttpContextBase context) 在 System.Web.Mvc.ControllerContext.get_DisplayMode() 结果消息:测试方法 OPSReviewTest._2A1ControllerTest.DetailsPrint_shouldPrint 抛出异常:System.NullReferenceException:对象引用未设置为对象的实例。

任何帮助或建议,谢谢。

4

2 回答 2

1

您正在尝试对您不拥有的代码进行单元测试?(耻辱,[钟声],耻辱......)

如果目标是单独测试控制器操作流,则建议抽象出第 3 方 PDF 生成,以便对其进行模拟以便于可测试性。

public interface IViewAsPdfWrapper {
    string ViewName { get; set; }
    object Model { get; set; }
    byte[] BuildPdf(ControllerContext context);
}

public class ViewAsPdfWrapper : IViewAsPdfWrapper {
    private readonly ViewAsPdf view;
    public ViewAsPdfWrapper() {
        view = new ViewAsPdf();
    }
    public string ViewName { get; set; }
    public object Model { get; set; }
    public byte[] BuildPdf(ControllerContext context) {
        view.ViewName = ViewName;
        view.Model = Model;
        return view.BuildPdf(context);
    }
}

现在可以将抽象注入到控制器中,以便根据需要为每个请求操作使用。

public class _2A1Controller : Controller {
    private readonly IUnitOfWork UnitOfWork;
    private readonly IViewAsPdfWrapper viewAsPdf;

    public _2A1Controller(IUnitOfWork uow, IViewAsPdfWrapper viewAsPdf) {
        this.UnitOfWork = uow;
        this.viewAsPdf = viewAsPdf;
    }

    public ActionResult DetailsPrint(int? id) {
        var a = viewAsPdf;
        a.ViewName = "../Ops/_2A1/Details";
        a.Model = UnitOfWork._2A1s.Get(id.Value);
        var pdfBytes = a.BuildPdf(ControllerContext);

        // return ActionResult
        MemoryStream ms = new MemoryStream(pdfBytes);
        return new FileStreamResult(ms, "application/pdf");
    }

}

现在单元测试可以安全地模拟第 3 方功能

public _2A1ControllerTest() {    
    _mockRepository = new Mock<I2A1Repository>();
    var mockUoW = new Mock<IUnitOfWork>();
    mockUoW.SetupGet(u => u._2A1s).Returns(_mockRepository.Object);

    var mockViewAsPdf = new Mock<IViewAsPdfWrapper>();
    mockViewAsPdf.Setup(m => m.BuildPdf(It.IsAny<ControllerContext>()))
        .Returns(() => new byte[0]);

    _mockRequest = new Mock<HttpRequestBase>();
    _mockHttpContext = new Mock<HttpContextBase>();
    _mockHttpContext.SetupGet(x => x.Request).Returns(_mockRequest.Object);

    _controller = new _2A1Controller(mockUoW.Object, mockViewAsPdf.Object);
    _controller.MockCurrentUser("test.admin");
    _controller.ControllerContext = new ControllerContext(_mockHttpContext.Object, new System.Web.Routing.RouteData(), _controller);

}

假设使用 FluentAssertions,测试方法应该是这样的(双关语:))

[TestMethod]
public void DetailsPrint_shouldPrint() {
    var result = _controller.DetailsPrint(1) as ActionResult;
    result.Should()
        .NotBeNull()
        .And
        .BeAssignableTo<ActionResult>();
}

最后,不要忘记在生产环境中使用 DI 容器注册接口及其实现。

于 2016-11-26T06:47:16.573 回答
0

您忘记分配DsiplayMode属性来ControllerContext添加这个:

_controller.ControllerContext.DisplayMode=_mockDisplayModeContext.Object;
于 2016-11-24T15:22:48.997 回答