Hi there,
I'm trying out the new mvc asp.net and want to get a basic unit test running for controller code. I have something like the following, but this gives me the following error:
Unable to cast object of type 'TypeMock.MockObject`1[System.Web.IHttpContext]' to type 'System.Web.IHttpContext'.
Does anyone have any experience of this?
Thanks
Graham
RouteTable.Routes.Add(new Route{ Url = "[controller]/[action]", RouteHandler = typeof(MvcRouteHandler)} );
RouteData routeData = new RouteData();
routeData.Values.Add("Action", "About");
routeData.Values.Add("Controller", "Home");
IHttpContext context = (IHttpContext)(MockManager.MockObject( typeof(IHttpContext ) ));
IHttpRequest request = (IHttpRequest)(MockManager.MockObject( typeof(IHttpRequest ) ));;
IHttpResponse response = (IHttpResponse)(MockManager.MockObject( typeof(IHttpResponse ) ));
IHttpSessionState session = (IHttpSessionState)(MockManager.MockObject( typeof(IHttpSessionState ) ));
IHttpServerUtility server = (IHttpServerUtility)(MockManager.MockObject( typeof(IHttpServerUtility ) ));
HomeController hc = new HomeController();
using (RecordExpectations recorder = RecorderManager.StartRecording())
{
recorder.ExpectAndReturn(request.ApplicationPath, "/");
recorder.ExpectAndReturn(context.Request, request);
recorder.ExpectAndReturn(context.Response, response);
recorder.ExpectAndReturn(context.Session, session);
recorder.ExpectAndReturn(context.Server, server);
// recorder.ExpectAndReturn(hc.RenderView, null); //Mock RenderView so tha it does notthing
}
hc.ControllerContext = new ControllerContext( context, routeData, hc);
hc.About();