【发布时间】:2020-04-29 11:40:20
【问题描述】:
是否有任何模式用于测试使用公共 api 中包含模板方法的类的类?我知道在动态多态模拟接口中是这样的解决方案:
struct Interface {
virtual void foo() = 0;
virtual ~Interface() = default;
};
class TestedClass {
public:
TestedClass(Interface& i) {}
// ... rest of the class
};
struct IMock : public Interface {
void foo() override {}
};
void test() {
IMock bar;
TestedClass baz(bar);
}
但是我可以用下面的东西做什么?有没有一种惯用的方法来测试这个?
struct Interface {
template<class T>
void foo() {
// do stuff depending on type
}
};
class TestedClass {
public:
TestedClass(Interface& i) {}
// ... rest of the class
// uses Interface foo with multiple types
};
【问题讨论】:
-
你测试模板,或者一个类,或者任何你想测试的东西,看看结果,你的问题到底是什么,不清楚。
标签: c++ unit-testing c++11