我有这样的代码:
class Foo
{
private :
int m_nValue;
public :
explicit Foo(const int nValue) :
m_nValue(nValue)
{
}
const int* Pointer() const
{
return &m_nValue;
}
};
void Test()
{
Foo obj(3);
// I want to do something like this.
//Assert::AreEqual(&obj.m_nValue, obj.Pointer());
}
我想测试Pointer()
返回私有字段地址的方法m_nValue
。有人可以为这种情况提供一些很好的解决方案。我可以让测试类/函数成为朋友,但我真的不喜欢它。
也许这不是应该测试的方法。但据我所知,所有公共方法都应该经过测试。
测试是用 C++ 编写的,用于 MSVS 2012 中的 C++ 代码