controllerContextをパラメータとして使用して、3番目のパートライブラリ "Rotativa"に基づいてpdfドキュメントを生成するアクションをコントローラでテストします。ここ
は、アクション(機能)の実装です:ユニットテスト用のControllerContextでDisplayModeをモックする方法c#
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 \コントローラがApi_2A1ControllerTest.cs \:ライン46 テスト結果:失敗 試験時間:0:04:39,3039007 結果のStackTrace:
System.Web.WebPages.DisplayでModeProvider.GetDisplayMode System.Web.Mvc.ControllerContext.get_DisplayMode() 結果メッセージにおいて(HttpContextBaseコンテキスト) : System.NullReferenceException:オブジェクト参照オブジェクトのインスタンスに設定されていないOPSReviewTest._2A1ControllerTest.DetailsPrint_shouldPrint例外が投げ 試験方法。
ありがとうございました。ありがとうございました。
正確にこのメソッドでテストしようとしていますか? – Nkosi