Xaisoft
Xaisoft

Reputation: 46591

Why is mocking preferred with Interfaces?

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?

Upvotes: 2

Views: 199

Answers (2)

Grzenio
Grzenio

Reputation: 36649

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.

Upvotes: 3

ktutnik
ktutnik

Reputation: 7262

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/

Upvotes: 1

Related Questions