Get Help:Ask a Question in our Forums|Report a Bug|More Help Resources
Last post Jan 10, 2008 09:35 AM by dagda2
Jan 08, 2008 02:15 PM|LINK
I am just writing my first unit test to simply test the index of my home controller.
There is a hell of a lot of interfaces I have to mock in order to run this test.
Has anybody any clues how to cut down the amount of code I will need to write.
So far I have this in my TestFixtureSetUp:
_mockContext = _mockRepository.DynamicMock<
_mockViewFactory = _mockRepository.DynamicMock<
And hen in my test I have:
homeController.ControllerContext = contextMock;
homeController.ViewFactory = _mockViewFactory;
That is a lot of mocking. Obviously I will write helper methods but it is quite a lot of boiler plate to be running each time.
Anybody come up with a good answer.
Jan 08, 2008 03:13 PM|LINK
Actually the test does not work. I get the following error:
Value cannot be null.
Parameter name: tempData
Can anyone show me how I can test RenderView from my controller?
Jan 08, 2008 05:00 PM|LINK
You will need to test against a subclass:
Jan 08, 2008 05:19 PM|LINK
I really do not like the subclass idea.
I found everything I need here:
The text is in Russian but the code obviously is not.
It sets TempData by using reflection like this:
The code was actually everything I needed.
Let me know what you think of it.
Jan 08, 2008 05:54 PM|LINK
I happen to know a little bit of Russian, the title says "ASP.NET MVC - when it all looks good only at first sight" [:)]
If we are resolved to use mocks at all costs, that's without no doubt brilliant solution. But subclass seems very clean and simple approach to me, I use it all the time and feel very comfortable. A matter of taste.
Jan 08, 2008 11:55 PM|LINK
Is Haack implying partial mocking for controllers will work in the next CTP?
I really prefer partial mocks cos they involve less coding, the subclass pattern requires too much duplicated code for each testable controller, it's a bit annoying.
Jan 09, 2008 06:34 AM|LINK
Yes, the next CTP should make the partial mock situation possible. No need for a test-specific subclass pattern.
Jan 10, 2008 09:35 AM|LINK
In the end I created my own mock class implentations for the HttpApplication, IHttpContext, IHttpRequest, IHttpResponse, IHttpServerUtility and IHttpSessionState.
I think this better than having to mock everything.
It gets messy when you have to it comes to replaying and verifying.
Maybe the framework could provide these mock implementations?