[英]“std::string” or “const std::string&” argument? (the argument is internally copied and modified)
[英]"Any value" for a `const std::string&` argument
我有一個將const std::string &
作為參數的函數。 我想寫這樣的東西:
EXPECT_CALL(mock, convertString(A<std::string>())).Times(0);
這編譯失敗:
no known conversion for argument 1 from 'testing::Matcher<std::basic_string<char> >' to 'const testing::Matcher<const std::basic_string<char>&>&'
我錯過了什么嗎?
這是 MCVE,對於那些傾向於實驗的人:
#include <string>
#include <gtest/gtest.h>
#include <gmock/gmock.h>
struct ToBeMocked {
virtual ~ToBeMocked() = default;
virtual void callMe(const std::string &arg) = 0;
};
struct Mock : public ToBeMocked {
MOCK_METHOD1(callMe, void (const std::string &arg));
};
TEST(Test, test)
{
Mock mock;
EXPECT_CALL(mock, callMe(::testing::An<std::string>()));
mock.callMe("aaa");
}
PS 我知道 StrictMock 解決方法,並將使用它。 但是...... A<T>()
不使用 const ref 參數似乎是一個疏忽......
答案很簡單:
EXPECT_CALL(mock, callMe(::testing::An<const std::string &>()));
使用函數參數的 EXACT 類型,而不是值類型。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.