You can mock it as follows and declare a stringBuilder object that accepts the output.
var response = new Mock<HttpResponseBase>(); response.Setup(x => x.Write(It.IsAny<string>())).Callback<string>(y => _stringBuilder.Append(y)); var url = new Uri("http://localhost/Home/"); var request = new Mock<HttpRequestBase>(); request.Setup(x => x.Url).Returns(url); request.Setup(x => x.ApplicationPath).Returns(""); var httpContext = new Mock<HttpContextBase>(); httpContext.Setup(x => x.Request).Returns(request.Object); httpContext.Setup(x => x.Response).Returns(response.Object); _controllerContext = new Mock<ControllerContext>(); _controllerContext.Setup(x => x.HttpContext).Returns(httpContext.Object); _homeController = autoMock.Create<HomeController>(); _homeController.ControllerContext = _controllerContext.Object;
You perform your action as follows:
var action=_homeController.Action(<parameters>); action.ExecuteResult();
and now your stringbuilder ie _stringBuilder object will produce the result, whatever it may be, and you can test it.
Baz1nga
source share