How do I mock a class without an interface?

前端 未结 8 1268
[愿得一人]
[愿得一人] 2020-11-30 23:50

I am working on .NET 4.0 using C# in Windows 7.

I want to test the communication between some methods using mock. The only problem is that I want to do it without i

8条回答
  •  遥遥无期
    2020-12-01 00:35

    Most mocking frameworks (Moq and RhinoMocks included) generate proxy classes as a substitute for your mocked class, and override the virtual methods with behavior that you define. Because of this, you can only mock interfaces, or virtual methods on concrete or abstract classes. Additionally, if you're mocking a concrete class, you almost always need to provide a parameterless constructor so that the mocking framework knows how to instantiate the class.

    Why the aversion to creating interfaces in your code?

提交回复
热议问题