簡體   English   中英

Gtest:如何在expect_call中每次根據增加的數字返回不同的字符串值?

[英]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"};

我的 gtest 將調用 function GetName()數百次。 所以我希望GetName()的嘲笑 function 可以像這樣返回:

int i = 0;
EXPECT_CALL(*my_mock, GetName()).Times(AtLeast(1)).WillRepeatedly(Return(tesName[(i++)%3]));

它總是可以返回名稱來自數組tesNameFinalPartialBest然后再次從Final開始。 但是上面的代碼不起作用。 我怎么能那樣做?

無論 i 是我的 gtest class 的局部變量還是成員變量,上面的代碼都不起作用。

在 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.

但我不知道如何將它應用到我的案例中。

如果您事先知道應該調用該方法的次數,則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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM