在C 中将模拟对象施加到其抽象基类
Cast mock objects to their abstract base class in C++
我的课程看起来像这样:
class TheClassIWantToTest {
public:
TheClassIWantToTest(const IInput& input) {
setLocalParameter(input.getParameter());
}
// other stuff, e.g. setLocalParameter, defined below
}
将输入参数定义为
class IInput {
virtual double getParameter() const = 0;
}
我还具有我在系统中使用的IInput
的实现,以及使用Google Mocks创建的模拟实现。
现在,我希望能够做
之类的事情MockInput mock; // MockInput : IInput
TheClassIWantToTest sut(mock);
在我的测试中,做
RealInput theRealStuff; // RealInput : IInput
TheClassIWantToTest(theRealStuff);
但是,当我尝试编译时,我会发现关于未定义TheClassIWantToTest(MockInput)
的错误。我试图为IInput
定义一个MockInput
的复制构建器,但是随后我获得了error: definition of implicitly-declared IInput(const MockInput&)
,因为我没有在类声明中定义方法。
但是,我宁愿避免在基类定义中声明复制构造函数,因为这意味着在我的生产代码中定义测试方法。(我意识到我可以通过使用IInput*
指针来解决此问题,但是如果可能的话,我也想避免使用。)
我无法想象我是第一个尝试实现这一目标的人,但是我无法找到该怎么做。有办法吗?如果是这样,您该怎么做?
尝试动态铸件:
RealInput theRealStuff; // RealInput : IInput
TheClassIWantToTest(dynamic_cast<const IInput&>(theRealStuff));
相关文章:
- 如何定义一个纯抽象基类
- 将抽象基类中的所有纯虚函数定义为 varaidaic 模板
- 在C++单元测试上下文中,抽象基类是否应将其他抽象基类作为函数参数
- 有没有一种方法可以使用SFINAE来检测一个类型是否实现了给定的抽象基类
- 自定义异常中的用户定义的空构造函数,具有多个继承和抽象基类
- 如何保存指向抽象基类的指针/引用,但在 c++ 中仍然可以复制
- C++:在共享对象中调用抽象基类构造函数/未定义的符号
- 从抽象基类中获取重载函数
- 如何构建一个 Map,其中键是抽象基类(而不是值)
- 无法调用纯抽象基类超载
- 私下继承C++抽象基类是什么意思
- 如何在抽象基类中实现运算符+
- 如何为抽象基类创建模板实例化?
- 崩溃 __cxxabiv1::__cxa_pure_virtual () - vtable ptr 到抽象基类
- CRTP - 是否可以创建一个抽象基类?
- 嵌套抽象基类的泛型模板
- 抽象基类的派生类未正确覆盖基纯虚拟方法
- 在C 中的数组中存储抽象基类的不同派生类的对象
- "Has a" C++关系,最佳做法是让一个类"implement"多个抽象基类?
- C++ 重新定义抽象基类