简体   繁体   English

`const std::string&` 参数的“任何值”

[英]"Any value" for a `const std::string&` argument

I have a function that takes const std::string & as an argument.我有一个将const std::string &作为参数的函数。 I'd like to write something like this:我想写这样的东西:

EXPECT_CALL(mock, convertString(A<std::string>())).Times(0);

This fails compilation:这编译失败:

no known conversion for argument 1 from 'testing::Matcher<std::basic_string<char> >' to 'const testing::Matcher<const std::basic_string<char>&>&'

Am I missing something?我错过了什么吗?

Here is the MCVE, for those inclined to experimment:这是 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 I am aware of the StrictMock workaround, and will use it. PS 我知道 StrictMock 解决方法,并将使用它。 But... A<T>() not working with const ref arguments seems like an oversight...但是...... A<T>()不使用 const ref 参数似乎是一个疏忽......

The answer turns out to be simple:答案很简单:

EXPECT_CALL(mock, callMe(::testing::An<const std::string &>()));

Use the EXACT type of the function argument, not the value type.使用函数参数的 EXACT 类型,而不是值类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM