[英]Gtest: how to return different string value based on an increasing number every time in the expect_call?
const std::wstring tesName[3] = { L"Final", L"Partial", L"Best"};
My gtest will call a function GetName()
hundreds of times.我的 gtest 将调用 function
GetName()
数百次。 So I hope that the mocked function of GetName()
can return like this:所以我希望
GetName()
的嘲笑 function 可以像这样返回:
int i = 0;
EXPECT_CALL(*my_mock, GetName()).Times(AtLeast(1)).WillRepeatedly(Return(tesName[(i++)%3]));
It can always returns the name comes from the array tesName
from Final
to Partial
to Best
then start from Final
again.它总是可以返回名称来自数组
tesName
从Final
到Partial
到Best
然后再次从Final
开始。 But the above code deson't work.但是上面的代码不起作用。 How could I do that?
我怎么能那样做?
No matter i is a local variable or a member variable of my gtest class, above code doesn't work.无论 i 是我的 gtest class 的局部变量还是成员变量,上面的代码都不起作用。
In gmock document:在 gmock 文档中:
using testing::ReturnPointee;
...
int x = 0;
MockFoo foo;
EXPECT_CALL(foo, GetValue())
.WillRepeatedly(ReturnPointee(&x)); // Note the & here.
x = 42;
EXPECT_EQ(42, foo.GetValue()); // This will succeed now.
But I don't know how to apply it to my case.但我不知道如何将它应用到我的案例中。
If you know beforehand the number of times n
the method should be called, you can do something like this:如果您事先知道应该调用该方法的次数,则
n
执行以下操作:
{
InSequence s;
for (int i = 0; i < n; i++) {
EXPECT_CALL(*my_mock, GetName())
.WillOnce(Return(tesName[i % 3]))
.RetiresOnSaturation();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.