简体   繁体   English

C ++ / GoogleTest-如何测试正在测试的类的成员变量

[英]C++ / GoogleTest - How to test member variable of a class being tested

I'm creating tests for a legacy code and wondering if it is possible to check the values of member variables of a class like this (I know my code below is very lousy, bad example :/. Hope to just please focus on the question): 我正在为遗留代码创建测试,并想知道是否可以检查像这样的类的成员变量的值(我知道我的下面的代码非常糟糕,示例很糟糕:/。希望仅关注此问题) ):

class Animal
{
public:
   RESULT getInfo();
   int age_;
};

int main()
{
   Animal animal;
   RESULT result = animal.getInfo();

   return 0;
}

RESULT Animal::getInfo()
{
    age_ = rand() % 10 + 1;
    if (age == 5)
    {
        return success;
    }
    else
    {
        return fail;
    }       
}

And in my test (using Google Test), I call getInfo(): 在测试中(使用Google测试),我调用getInfo():

EXPECT_EQ(success, sut_->getInfo());

However, this just verifies that the result of getInfo() is success . 但是,这只是验证了getInfo()的结果是否为success Is there any other way for me to check the value of age_ without adding a new method/changing the return value? 我还有其他方法可以检查age_的值而无需添加新方法/更改返回值吗? Thanks! 谢谢!

As you have already made age_ public, you can just add another EXPECT_EQ statement. 由于已经公开age_ ,因此您只需添加另一个EXPECT_EQ语句即可。 If making age_ public was not intentional then you will have to come with a method to access age_ in GTest code. 如果不打算公开age_那么您将必须提供一种在GTest代码中访问age_的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM