Looking for direction on unit testing a controller extension that renders a partial view

前端 未结 1 918
后悔当初
后悔当初 2020-12-31 08:43

As the title says, I\'m looking for direction on how to properly test a controller extension. The extension renders a partial view which in turn I\'m using within a JSONResu

相关标签:
1条回答
  • 2020-12-31 09:17

    You will have to add a mocked view engine to the ViewEngines.Engines collection so that you can mock the FindPartialView call. Here's an example with Rhino Mocks:

    var view = MockRepository.GenerateStub<IView>();
    var engine = MockRepository.GenerateStub<IViewEngine>();
    var viewEngineResult = new ViewEngineResult(view, engine);
    engine
        .Stub(x => x.FindPartialView(null, null, false))
        .IgnoreArguments()
        .Return(viewEngineResult);
    ViewEngines.Engines.Add(engine);
    

    Then you could assert that the view.Render method was called, intercept its arguments and write some mocked data to this writer and finally assert that your controller action returned this mocked string.

    0 讨论(0)
提交回复
热议问题