Search code examples
c++c++14googletestgooglemock

Save a char* parameter intro a string


Please help me with this question.. I'm beginner with gtest. I have a mocked function

DoSomething(const char* par0, const char* par2)

I want to save its second argument into

    std::string `savedPar_`;

EXPECT_CALL(mockd_, DoSomething(_, _,))
        .WillOnce(DoAll(SaveArg<1>(savedPar_), (Return(Ok))));

And got this error:

error: no match for ‘operator*’ (operand type is ‘const std::__cxx11::basic_string<char>’)
   *pointer = ::testing::get<k>(args);

Than you so much in advance!


Solution

  • According to the doc

    SaveArg<N>(pointer) Save the N-th (0-based) argument to *pointer.

    It should be:

    std::string savedPar_;
    
    EXPECT_CALL(mockd_, DoSomething(_, _,))
            .WillOnce(DoAll(SaveArg<1>(&savedPar_), (Return(Ok))));
    //                                 ^