問題
我需要幫助來解決我的 gtest 1.10.0 版本的單元測驗問題。當我嘗試對涉及接受 std::experimental::any 引數的函式進行單元測驗時,會引發例外并終止單元測驗。
重現問題的步驟
覆寫我的測驗場景的單元測驗片段可在https://godbolt.org/z/Y7dvEsaPf 在 TestBoth 測驗用例中,如果相鄰提供 EXPECT_CALL 和實際函式呼叫,則不會拋出例外并且測驗用例成功執行。但是在我的實際專案代碼中,我的測驗函式呼叫了具有這兩種資料型別的 send_data() 函式。
工具和作業系統版本 gtest 版本為 1.10.0 Ubuntu Linux 20.04
編譯器版本
g (Ubuntu 10.3.0-1ubuntu1~20.04) 10.3.0 C 14
構建系統
cmake 版本 3.20.5
附加背景關系
需要幫助或請直接到我可以詢問此查詢并得到解決的地方。
uj5u.com熱心網友回復:
問題是AnyMatcher成功匹配 any std::any。解決方案是通過以下方式強制實作進一步的期望::testing::InSequence:
TEST(MockUseWith, TestBoth)
{
TestMock mock;
InSequence seq;
EXPECT_CALL(mock, send_data(AnyMatcher(EnableReq{true})));
EXPECT_CALL(mock, send_data(AnyMatcher(ReadReq())));
mock.send_data(EnableReq{true});
mock.send_data(ReadReq{});
}
https://godbolt.org/z/eaj4Pxb1T
[==========] Running 2 tests from 1 test suite.
[----------] Global test environment set-up.
[----------] 2 tests from MockUseWith
[ RUN ] MockUseWith.TestBoth
[ OK ] MockUseWith.TestBoth (0 ms)
[ RUN ] MockUseWith.TestOne
[ OK ] MockUseWith.TestOne (0 ms)
[----------] 2 tests from MockUseWith (0 ms total)
[----------] Global test environment tear-down
[==========] 2 tests from 1 test suite ran. (1 ms total)
[ PASSED ] 2 tests.
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/427895.html
