Using SetArgumentPointee

2,036 views
Skip to first unread message

mikeb

unread,
Jan 21, 2011, 7:13:47 PM1/21/11
to Google C++ Mocking Framework
Hi,

I'm attempting to use SetArgumentPointee to change the value of an
argument when a method is called:

<code>
const string ip = "10.153.2.1";
string cmd = "configuration";
string responseIn;
string responseOut = "12345678901234567890";
string error = "";

int retInt = 0;
EXPECT_CALL(mdaf, sendCommand(ip, cmd, responseIn,
error)).WillOnce(DoAll(SetArgumentPointee<2>(responseOut),
Return(retInt)));
</code>

However, I am getting a compiler or linker error:

<code>
GTestArrayCLICommand.cpp:97: warning: deprecated conversion from
string constant to 'char*'
In file included from /usr/local/include/gmock/gmock.h:58,
from GTestArrayCLICommand.cpp:3:
/usr/local/include/gmock/gmock-actions.h: In member function 'void
testing::internal::SetArgumentPointeeAction<N, A,
kIsProto>::Perform(const ArgumentTuple&) const [with Result = void,
ArgumentTuple = std::tr1::tuple<const std::string&, std::string&,
std::string&, std::string&>, long unsigned int N = 2ul, A =
std::basic_string<char, std::char_traits<char>, std::allocator<char>
>, bool kIsProto = false]':
/usr/local/include/gmock/gmock-actions.h:380: instantiated from
'typename testing::internal::Function<F>::Result
testing::PolymorphicAction<Impl>::MonomorphicImpl<F>::Perform(const
typename testing::internal::Function<F>::ArgumentTuple&) [with F =
void(const std::string&, std::string&, std::string&, std::string&),
Impl = testing::internal::SetArgumentPointeeAction<2ul,
std::basic_string<char, std::char_traits<char>, std::allocator<char>
>, false>]'
GTestArrayCLICommand.cpp:105: instantiated from here
/usr/local/include/gmock/gmock-actions.h:684: error: no match for
'operator*' in '*std::tr1::get [with int __i = 2, _Elements = const
std::string&, std::string&, std::string&, std::string&](((const
std::tr1::tuple<const std::string&, std::string&, std::string&,
std::string&>&)((const std::tr1::tuple<const std::string&,
std::string&, std::string&, std::string&>*)args)))'
</code>

I am sortof assuming this is due to the fact that responseOut is not a
pointer arg. If this is the case, is there a way to influence a non-
pointer arg?

Magnificent test/mock framework implementation, by the way. I switched
over to it from boost::test in a very short period of time and have
introduced it to the rest of my team.

...Mike

Vlad Losev

unread,
Jan 21, 2011, 7:35:21 PM1/21/11
to mikeb, Google C++ Mocking Framework
Mike,

From the error, it looks like the second argument of sendCommand is a reference rather than a pointer, and thus SetArgumentPointee cannot dereference it for assignment (BTW, use SetArgPointee, the SetArgumentPointee name is deprecated). If this is the case, you should use SetArgReferee instead.
 
 
Magnificent test/mock framework implementation, by the way. I switched
over to it from boost::test in a very short period of time and have
introduced it to the rest of my team.

...Mike


HTH,
Vlad

Steve Fox

unread,
Jan 21, 2011, 11:26:10 PM1/21/11
to Vlad Losev, mikeb, Google C++ Mocking Framework
On Fri, Jan 21, 2011 at 6:35 PM, Vlad Losev <vl...@losev.com> wrote:
> Mike,
> On Fri, Jan 21, 2011 at 4:13 PM, mikeb <broa...@gmail.com> wrote:
>>        EXPECT_CALL(mdaf, sendCommand(ip, cmd, responseIn,
>> error)).WillOnce(DoAll(SetArgumentPointee<2>(responseOut),
>> Return(retInt)));
>> </code>

> From the error, it looks like the second argument of sendCommand is a


> reference rather than a pointer, and thus SetArgumentPointee cannot
> dereference it for assignment

Judging from the "conversion from string constant to 'char*' message,
he'll probably need to change the third argument from char* to a
string reference, right?

I also noticed that responseOut wasn't included in the function
argument list, but responseIn is. Maybe that was a typo, but SetArg*
can't affect responseOut since it wasn't passed in.

Best of luck.
Steve

mikeb

unread,
Jan 24, 2011, 12:22:43 PM1/24/11
to Google C++ Mocking Framework
SetArgReferee worked perfectly.

Thanks!
Reply all
Reply to author
Forward
0 new messages