简体   繁体   中英

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

I have a function that takes const std::string & as an argument. 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:

#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. But... A<T>() not working with const ref arguments seems like an oversight...

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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