views:

101

answers:

2

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
+3  A: 

Because:

  1. All methods on your object would need to be virtual in order to mock them
  2. 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