Can you mock an object that implements an interface AND an abstract class?

前端 未结 2 554
余生分开走
余生分开走 2021-02-14 01:12

Is it possible to use Moq to mock an object that implements an interface and abstract class?

I.e.:

public class MyClass: SomeAbstractClass, IMyClass
         


        
2条回答
  •  粉色の甜心
    2021-02-14 02:09

    Your question does not really make sense. Moq can be used to mock abstract classes and interfaces. Since MyClass is neither then you cannot create a mock of it. I don't think this is a problem though, since consumers of your MyClass instance will probably be expecting a SomeAbstractClass or an IMyClass instance and not a MyClass instance. If indeed they expect a MyClass instance, then you need to make some abstraction on top of it. This can be achieved by either having SomeAbstractClass implement the IMyClass interface, or by having the IMyClass interface expose the methods and properties of SomeAbstractClass.

提交回复
热议问题