gmock failed to pass values to my function

421 views
Skip to first unread message

Han Blues

unread,
Mar 6, 2014, 1:26:00 AM3/6/14
to googl...@googlegroups.com

I am using gmock in my project and I meet a problem to set a custom reference variable for a mock function. I have used SetArgPointee and Return ,but the function can only get the return value.

my mock function is

MOCK_METHOD1_WITH_CALLTYPE(__stdcall, get_length, HRESULT(long *p));

my TEST is like this:

long pl = 10;    
EXPECT_CALL(*ihtmlelemc, get_length(_)).WillOnce(DoAll(SetArgPointee<0>(pl), Return(S_OK)));

and my test function is

long len = 0;
HRESULT hr;

hr = pObjCollect->get_length(&len);
if (hr != S_OK || len == 0)
{
    return E_NOINTERFACE;
}

when I run the test only found that thr hr is S_OK, len is still 0, the len can't get the value,anyone can tell me why and how to get the value,thanks.


Billy Donahue

unread,
Mar 6, 2014, 8:18:51 AM3/6/14
to Han Blues, googl...@googlegroups.com
First double check that that mock method is being called at all.
It could be that there's something else going on or some typo.
Need to narrow down the trouble.
I don't for example see the definition of pObjCollect in your sample code.

--

---
You received this message because you are subscribed to the Google Groups "Google C++ Mocking Framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to googlemock+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/googlemock/d6aba138-f995-4b73-9fef-227158cdbc39%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Han Blues

unread,
Mar 6, 2014, 10:34:24 PM3/6/14
to googl...@googlegroups.com, Han Blues
Great thks for your reply,I get the value by using ACTION_P like this *arg0 = value and return S_OK, I think I have used a wrong type.
By the way,I want to ask another question.
my function is 
BOOL GetElement(IHTMLDocument2 * pdocument)
{
        IHTMLDocument3* pdoc3;
        pdocument->QueryInterface(IID_IHTMLDocument3, (VOID **) &pdoc3);
        .....
}
I mocked the IHTMLDocument2 with MockIHTML2, and initialized by ATL:CComObject.I have defined a mocked QueryInterface.
But when I test the GetElement following:
TEST_F(test, testelement)
{
      MockIHTML2 * ihtml2; 
      .....//initialize
      EXPECT_CALL(*ihtml2, QueryInterface(_, _)).WillOnce(Return S_OK);
      EXPECT_TRUE(GetElement(ihtml2));
}
I debug the code and found when goto  pdocument->QueryInterface, it have called the QueryInterface in ATL:CComObject instead of my mock;
Only when I change the function ,it can call my mock funtion.like this
BOOL GetElement(MockIHTML2* pdocument)
{
        IHTMLDocument3* pdoc3;
        pdocument->MockIHTML2::QueryInterface(IID_IHTMLDocument3, (VOID **) &pdoc3);
        .....
}


when I debug the function ,I got the pdocument list two type.like this

I want to call the mock funtion,but it called the ATL funtion by default.
Is there any way to get the mock function without changing the original function.


On Thursday, March 6, 2014 9:18:51 PM UTC+8, Billy Donahue wrote:
First double check that that mock method is being called at all.
It could be that there's something else going on or some typo.
Need to narrow down the trouble.
I don't for example see the definition of pObjCollect in your sample code.


On Thursday, March 6, 2014, Han Blues <hbq...@gmail.com> wrote:

I am using gmock in my project and I meet a problem to set a custom reference variable for a mock function. I have used SetArgPointee and Return ,but the function can only get the return value.

my mock function is

MOCK_METHOD1_WITH_CALLTYPE(__stdcall, get_length, HRESULT(long *p));

my TEST is like this:

long pl = 10;    
EXPECT_CALL(*ihtmlelemc, get_length(_)).WillOnce(DoAll(SetArgPointee<0>(pl), Return(S_OK)));

and my test function is

long len = 0;
HRESULT hr;

hr = pObjCollect->get_length(&len);
if (hr != S_OK || len == 0)
{
    return E_NOINTERFACE;
}

when I run the test only found that thr hr is S_OK, len is still 0, the len can't get the value,anyone can tell me why and how to get the value,thanks.


--

---
You received this message because you are subscribed to the Google Groups "Google C++ Mocking Framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to googlemock+unsubscribe@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages