gpt4 book ai didi

c++ - 如何使用 googletest/googlemock 框架测试连续/相关的更改?

转载 作者:太空宇宙 更新时间:2023-11-04 14:33:32 24 4
gpt4 key购买 nike

我正在尝试使用 googletest/googlemock 测试类 Foo 以返回其方法状态。据我了解,对于方法 counter() 的每次调用,m_counter 的值应该增加 1,但是我总是得到一个或相同的结果,即 m_counter 永远不会达到限制。这是否意味着每次调用都会清除所有数据?有人可以告诉我如何使用谷歌测试来测试连续/相关的更改吗?看起来每次调用 counter() 都是它自己的故事(不相关),即使对象相同?

class Foo
{
public:
Foo() { m_counter = 0;}
void counter() { m_counter++; }
int getCounter() { return m_counter;}
int status() { return (m_counter>=5)?0:1;}
private:
int m_counter;
};

TEST_F(TestFoo, TestStatus_1)
{
Foo fooP;
fooP.counter();
ASSERT_TRUE(fooP.status() == 0);
std::cout<<fooP.getCounter()<<std::endl; //should be 1
fooP.counter();
ASSERT_TRUE(fooP.status() == 0);
std::cout<<fooP.getCounter()<<std::endl; //should be 2
fooP.counter();
ASSERT_TRUE(fooP.status() == 0);
std::cout<<fooP.getCounter()<<std::endl; //should be 3
fooP.counter();
ASSERT_TRUE(fooP.status() == 0);
std::cout<<fooP.getCounter()<<std::endl; //should be 4
fooP.counter();
ASSERT_TRUE(fooP.status() == 0);
std::cout<<fooP.getCounter()<<std::endl; //should be 5
fooP.counter();
ASSERT_TRUE(fooP.status() == 1);
std::cout<<fooP.getCounter()<<std::endl; //should be 6

}

最佳答案

代码错误:

int status()     { return (m_counter>=5)?0:1;}

应该是:

int status()     { return (m_counter<=5)?0:1;}

关于c++ - 如何使用 googletest/googlemock 框架测试连续/相关的更改?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30483611/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com