chevron-thin-right chevron-thin-left brand cancel-circle search youtube-icon google-plus-icon linkedin-icon facebook-icon twitter-icon toolbox download check linkedin phone twitter-old google-plus facebook profile-male chat calendar profile-male
Welcome to Typemock Community! Here you can ask and receive answers from other community members. If you liked or disliked an answer or thread: react with an up- or downvote.
0 votes
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();
asked by gjpeden (640 points)

4 Answers

0 votes
hi,

To access the created instance of the MockObject, you need to use the Object property.

meaning that the following lines:
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 ) )); 


should be written as:
IHttpRequest request = (IHttpRequest)(MockManager.MockObject( typeof(IHttpRequest )).Object); 
IHttpResponse response = (IHttpResponse)(MockManager.MockObject( typeof(IHttpResponse )).Object); 
IHttpSessionState session = (IHttpSessionState)(MockManager.MockObject( typeof(IHttpSessionState )).Object); 
IHttpServerUtility server = (IHttpServerUtility)(MockManager.MockObject( typeof(IHttpServerUtility)).Object);

this is the reason you get the casting error.

I've recently blogged about usage of the MockObject API here.
answered by lior (13.2k points)
0 votes
That works fine - Many thanks
answered by gjpeden (640 points)
0 votes
You can tighten it up a bit and make it more readable using the generics syntax, like:

IHttpRequest request = MockManager.MockObject<IHttpRequest>().Object;
answered by tillig (6.7k points)
0 votes
FYI, We've just released an MvcMockHelpers.cs class that can help with the scenario you are facing. using it you can write unit tests for mvc controllers quite easily:

http://blog.typemock.com/2008/03/testin ... emock.html
HTH
Roy
answered by royo (2k points)
...