nUnit testing a controller extension method

对着背影说爱祢 提交于 2019-12-02 13:24:55

It appears the subject under test may be tightly coupled to 3rd party implementation concerns that make testing it in isolation difficult.

I suggest mocking the view render abstraction referred to in your original statement

public interface IViewRender {
    string RenderPartialView(Controller controller, string viewName, object model = null, bool removeWhiteSpace = true);
}

to return a string when invoked so that the method under test can flow to completion and you can assert you expected behavior.

//Arrange

//...

var viewRenderMock = new Mock<IViewRender>(); //Using Moq mocking framework
viewRenderMock
    .Setup(_ => _.RenderPartialView(It.IsAny<Controller>(), It.IsAny<string>(), It.IsAny<object>(), true))
    .Returns("some fake view string");

//...

var controller = new SyllabusAjaxListController(viewRenderMock.Object,...) {
    //...
};

//Act
var result = controller.Create(validModel) as JsonResult;

//Assert
result.Should().NotBeNull(); //FluentAssertions

//...other assertions.
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!