I have been looking at examples of mocking using Moq and Rhino Mocks and all the examples seem to mock interfaces. Why is this? I have heard they can mock static classes, but what about non-static classes?
+1
A:
Because that was limitation of DynamicProxy (used to create instance of the mock object). Will be no problem if you using interface, but If you want to use class then you should make all public member virtual. http://tiredblogger.wordpress.com/2008/05/06/moq-mocks-use-virtual-method-or-interfaces/
ktutnik
2010-09-04 03:52:53
+3
A:
Because:
- All methods on your object would need to be virtual in order to mock them
- Your mock would still need to execute the constructor of the real object, pass parameters and run the logic inside it, which can be painful.
Grzenio
2010-09-06 09:52:22