[英]How to test a contiguous/related changes using googletest/googlemock framework?
I am trying to test class Foo for return of it's method status, by using googletest/googlemock. 我正在尝试通过使用googletest / googlemock测试Foo类是否返回其方法状态。 To my understanding for every call of the method counter()
the value of m_counter should be increasing by one, however I am always getting one or same result, ie m_counter never reaches the limit. 据我了解,对于方法counter()
的每次调用, m_counter的值应增加一,但是我总是得到一个或相同的结果,即m_counter从未达到极限。 Does it mean all data are cleared for every call? 这是否意味着每次通话都会清除所有数据? Can someone tell help me how to test a contiguous/related changes using google test? 有人可以告诉我如何使用Google测试来测试连续/相关的更改吗? It looks Every call to counter()
is it's own story (not related), even if the object is same? 看起来每个对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
}
Bug in code: 代码错误:
int status() { return (m_counter>=5)?0:1;}
should be: 应该:
int status() { return (m_counter<=5)?0:1;}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.