I did a search on SO and looks like this question gets asked quite often. I have been able to get the mocks working and I'm also able to execute OnActionExecuted() without any issues. Here's my Unit Test. The commented lines are the ones that fail and I'm sure I'm not mocking the right type.
//Arrange
//var viewResult = new ViewResult();
var filterContextMock = new Mock<ActionExecutedContext>();
var routeData = new RouteData();
var httpContextMock = new Mock<HttpContextBase>();
routeData.Values["data"] = "Mock data";
var requestContext = new RequestContext(httpContextMock.Object, routeData);
var controller = new FakeController();
controller.ControllerContext = new ControllerContext(requestContext, controller);
filterContextMock.Setup(f => f.RouteData).Returns(routeData);
filterContextMock.Setup(f => f.Controller).Returns(controller);
//filterContextMock.Setup(f => f.Result).Returns(viewResult);
//Act
var wrapFilterAttribute = new WrapFilterAttribute();
wrapFilterAttribute.OnActionExecuted(filterContextMock.Object);
Here is my Action Filter.
public class WrapFilterAttribute : ActionFilterAttribute
{
public override void OnActionExecuted(ActionExecutedContext filterContext)
{
var view = (ViewResultBase)filterContext.Result;
if (view != null)
{
BaseViewModel viewModel = (BaseViewModel)view.ViewData.Model ?? new BaseViewModel();
viewModel.Wrap = new WrapperFactory().GetWrap();
}
base.OnActionExecuted(filterContext);
}
}
The issue I'm facing here is filterContext.Result always comes in as EmptyResult. I'd like to push in a hydrated ViewResult instead. Any ideas how I can accomplish this?
Many thanks!