How do you prevent the base class constructor from

2019-07-13 03:39发布

How do you prevent the base class constructor from being called with Moq?

I'm unable to Mock an object with Moq because the base classes constructor is being called and it requires real objects, so I want to stop the base class constructor from being called.

var parametersMoq = new Mock<MyDerivedClass>(null, "Params", null){ CallBase = false, };
_storedProcedureAccessor._parameters = parametersMoq.Object;

MyDerivedClass's base class constructor is causing me issues.

1条回答
闹够了就滚
2楼-- · 2019-07-13 03:56

There is no way to prevent the base class constructor from being invoked.

If you can edit the base class, you should replace the fixed dependencies with abstractions (e.g. an interface, abstract class or a delegate).

If you can't edit the base class, and you really need to be able to substitute the dependencies with test friendly fakes to write your unit tests, you need to do a bit more work (e.g. wrap the problematic base class in an abstraction, then use composition instead of inheritence, and depend on the new abstraction).

查看更多
登录 后发表回答