如何在 ControllerContext 中模拟 DisplayMode 以进行单元测试 c#

How to mock a DisplayMode in ControllerContext For Unit Test c#

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

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");

}

下面是我试图让单元测试工作的方法:

Test Name: DetailsPrint_shouldPrint Test FullName: OPSReviewTest._2A1ControllerTest.DetailsPrint_shouldPrint Test Source: C:\inetpub\wwwroot\OpsReview\OPSReviewTest\Controllers\Api_2A1ControllerTest.cs : line 46 Test Outcome: Failed Test Duration: 0:04:39,3039007 Result StackTrace:
at System.Web.WebPages.DisplayModeProvider.GetDisplayMode(HttpContextBase context) at System.Web.Mvc.ControllerContext.get_DisplayMode() Result Message: Test method OPSReviewTest._2A1ControllerTest.DetailsPrint_shouldPrint threw exception: System.NullReferenceException: Object reference not set to an instance of an object.

任何帮助或建议,谢谢。

您忘记将 DsiplayMode 属性 分配给 ControllerContext 添加:

_controller.ControllerContext.DisplayMode=_mockDisplayModeContext.Object;

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

如果目标是单独测试控制器操作流程,那么建议抽象出第 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 容器注册接口及其实现。