mocking a function taking a reference

17 views
Skip to first unread message

jsrookie

unread,
Apr 19, 2011, 1:22:41 PM4/19/11
to gmock-user
Hi,

Can anyone help me mock a function taking a reference and returning a
bool?
I use ::testing::InvokeArgument but it seem like I miss-use it.
The below is the source code and the function I am trying to mock is
"MockMe::IncrementFunction"

Please shed a light on me.
Thank you,
-JS

------------------------------------------------------------------------------------------------------
class ImAClass
{
public:
int a;
int b;

ImAClass():a(0), b(0){};
~ImAClass(){};
};

class MockMe
{
public:
bool IncrementFunction(int v, ImAClass& imAClass);
};

class MockMockMe : MockMe
{
public:
MOCK_METHOD2(IncrementFunction, bool(int v, ImAClass&
imAClass));
};

class IncrementTest : public ::testing::Test
{
public:
MockMockMe m_MockMockMe;
};


bool MockMe::IncrementFunction(int v, ImAClass& imAClass)
{
imAClass.a += v;
imAClass.b += v;

// do something here more

return true;
}



TEST_F(IncrementTest, test_success)
{
int id = 5;

ImAClass imAClass;
imAClass.a = 10;
imAClass.b = 12;

ON_CALL(m_MockMockMe, IncrementFunction( id, ::testing::_ ))
.WillByDefault( DoAll(::testing::InvokeArgument<1>(::testing::ByRef(imAClass))
, ::testing::Return (id%2)));

EXPECT_TRUE(m_MockMockMe.IncrementFunction(id, imAClass));


}
---------------------------------------------------------------------------------------------------------------

Johnny Jian

unread,
Apr 19, 2011, 10:05:54 PM4/19/11
to gmock...@googlegroups.com
I am afraid that you mean "Google Mock". But it is the mailing list of "Groovy Mock".

Johnny
2011-4-20


--
You received this message because you are subscribed to the Google Groups "gmock-user" group.
To post to this group, send email to gmock...@googlegroups.com.
To unsubscribe from this group, send email to gmock-user+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/gmock-user?hl=en.


jsrookie

unread,
Apr 21, 2011, 9:06:27 AM4/21/11
to gmock-user
Oops. sorry. Wrong door ;-)

On Apr 19, 9:05 pm, Johnny Jian <johnnyjia...@gmail.com> wrote:
> I am afraid that you mean "Google Mock". But it is the mailing list of
> "Groovy Mock".
>
> Johnny
> 2011-4-20On Wed, Apr 20, 2011 at 1:22 AM, jsrookie <jsroo...@gmail.com> wrote:
> > Hi,
>
> > Can anyone help me mock a function taking a reference and returning a
> > bool?
> > I use ::testing::InvokeArgument but it seem like I miss-use it.
> > The below is the source code and the function I am trying to mock is
> > "MockMe::IncrementFunction"
>
> > Please shed a light on me.
> > Thank you,
> > -JS
>
> > ---------------------------------------------------------------------------­---------------------------
> > ---------------------------------------------------------------------------­------------------------------------
Reply all
Reply to author
Forward
0 new messages