views:

88

answers:

2

I'm using this kind of wiring for my MVC and I want to test the controller. So far, I've found two ways : either my mock view dispatch events or my controller expose public methods.

Dispatching events seems the way to go (as pointed out in an answer), but if my view is a simple mock object, how do I dispatch those events?

+1  A: 

If your view normally dispatches events then the best thing is to have your mock objects also do that. The mock objects are meant to mock the interface and dispatched events are part of that interface (although not always a well documented and visible part). It also means you are testing the controllers in the same fashion as they will be used.

Of course, if your view doesn't dispatch events normally then I'd guess it normally uses public methods and you should do the same.

workmad3
But then, how do I trigger those events if my view is "dumb"?
Subb
It means adding some extra stuff to your mocks, but I'd go for having methods that can trigger certain events which you'd then call from your tests. 'viewMock.dispatchClickEvent()' or similar.
workmad3
A: 

Since you haven't tagged the question with a language, I don't know if this will work, but have you considered using reflection and exercising the protected/private controller methods that way? In my view you should be testing that the method operates correctly, you can do this by invoking it via reflection if your language supports it. You'd need other tests to make sure that your event handlers are set up properly or if you have code that you've written that dispatches the events, you'd need to test that the events are properly dispatched.

tvanfosson