【发布时间】:2011-12-23 17:03:35
【问题描述】:
我有一个单元测试类Tester;我希望它访问 Working 类的私有字段。
class Working {
// ...
private:
int m_variable;
};
class Tester {
void testVariable() {
Working w;
test( w.m_variable );
}
}
我有以下选择:
- 使 m_variable
public- 丑陋 - 制作方法
test_getVariable()- 过于复杂 - 将
friend class Tester添加到 Working - 然后 Working 明确地“了解”测试人员,这不好
我的理想是
class Working {
// ...
private:
int m_variable;
friend class TestBase;
};
class TestBase {};
class Tester : public TestBase {
void testVariable() {
Working w;
test( w.m_variable );
}
}
Working 知道 TestBase 但不是每个测试...但它不起作用。显然,友谊不适用于继承。
这里最优雅的解决方案是什么?
【问题讨论】:
标签: c++ unit-testing