Need help mocking a ASP.NET Controller in Rhino Mocks

Posted by Pure.Krome on Stack Overflow See other posts from Stack Overflow or by Pure.Krome
Published on 2010-05-24T14:33:13Z Indexed on 2010/05/25 1:21 UTC
Read the original article Hit count: 246

Hi folks,

I'm trying to mock up a fake ASP.NET Controller. I don't have any concrete controllers, so I was hoping to just mock a Controller and it will work.

This is what I currently have:

_fakeRequestBase = MockRepository.GenerateMock<HttpRequestBase>();
_fakeRequestBase.Stub(x => x.HttpMethod).Return("GET");
_fakeContextBase = MockRepository.GenerateMock<HttpContextBase>();
_fakeContextBase.Stub(x => x.Request).Return(_fakeRequestBase);

var controllerContext = new ControllerContext(_fakeContextBase, new RouteData(),
    MockRepository.GenerateMock<ControllerBase>());
_fakeController = MockRepository.GenerateMock<Controller>();
_fakeController.Stub(x => x.ControllerContext).Return(controllerContext);

Everything works except the last line, which throws a runtime error and is asking me for some Rhino.Mocks source code or something (which I don't have).

See how I'm trying to mock up an abstract Controller - is that allowed?

Can someone help me?

© Stack Overflow or respective owner

Related posts about asp.net-mvc

Related posts about unit-testing