如何防止Moq调用基类构造函数? [英] How do you prevent the base class constructor from being called with Moq?

查看:56
本文介绍了如何防止Moq调用基类构造函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何防止Moq调用基类构造函数?

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

我无法用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的基类构造函数导致我遇到问题.

MyDerivedClass's base class constructor is causing me issues.

推荐答案

无法阻止基类构造函数被调用.

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).

这篇关于如何防止Moq调用基类构造函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆