how can I use google mock to introduce stub function?

2,958 views
Skip to first unread message

Rasconi

unread,
Jan 8, 2009, 3:38:16 AM1/8/09
to Google C++ Mocking Framework
Hi,

If I want to use a stub function during test because some parts of
exterior code which used by my code is not ready, how can I get it
with google mock?

even it is well known situation from unit test, i explain it


//my code
...
loadsynch()
{
a = b + Kernel.getsynch(); // body of the Kernel class is not
present except the prototype declaration
...
}


then, how can i take a stub for Kernel.getsynch() and allocate default
return value in the unit test with google test & mock?

for example,

if i want to write the test code like this : EXPECT_CALL( 3,
mycode.loadsynch());


cheol

Pavol Murin

unread,
Jan 8, 2009, 3:58:07 AM1/8/09
to Rasconi, Google C++ Mocking Framework
On Thu, Jan 8, 2009 at 09:38, Rasconi <cho...@gmail.com> wrote:
>
>
> Hi,
>
> If I want to use a stub function during test because some parts of
> exterior code which used by my code is not ready, how can I get it
> with google mock?

Hi cheol,

One way to do it is to define a mock class:

class MockKernel : public Kernel {
public:
// constructor etc.

MOCK_METHOD0(getsynch, int());
};

then use an instance of the MockKernel instead of the real Kernel. You
can create an instance of the MockKernel in your test case and set
expectations and return values as needed:

TEST(Test, Case) {
MockKernel mock_kernel;
EXPECT_CALL(mock_kernel, getsynch).WillOnce(Return(0));

EXPECT_CALL(mycode.loadsynch());

// mycode must use the mock kernel instance:
mycode.set_kernel(mock_kernel);

// execute code under test:
mycode.loadsynch();
}

hope this helps, Pavol

Chris Henry

unread,
Jan 8, 2009, 3:55:12 PM1/8/09
to Pavol Murin, Rasconi, Google C++ Mocking Framework
On Thu, Jan 8, 2009 at 4:58 PM, Pavol Murin <pmu...@google.com> wrote:
>
> On Thu, Jan 8, 2009 at 09:38, Rasconi <cho...@gmail.com> wrote:
[...]

>> If I want to use a stub function during test because some parts of
>> exterior code which used by my code is not ready, how can I get it
>> with google mock?
>
[...]

> One way to do it is to define a mock class:
>
> class MockKernel : public Kernel {
> public:
> // constructor etc.
>
> MOCK_METHOD0(getsynch, int());
> };
>
> then use an instance of the MockKernel instead of the real Kernel. You

In addition, you would probably want to delegate to the real object
for the remaining methods (if you needed to and if you want to verify
the possible side-effects of the calls to Kernel's methods).

http://code.google.com/p/googlemock/wiki/CookBook#Delegating_Calls_to_a_Real_Object


--
Chris
chrish...@gmail.com
+65 9755 3292 (1 more week to Singapore!)

A: Because it fouls the order in which people normally read text.
Q: Why is top-posting such a bad thing?

Rasconi

unread,
Jan 8, 2009, 11:22:45 PM1/8/09
to Google C++ Mocking Framework
Yeah. I just figured out how the MOCK is working on the code and test.

Thx. Pavol and Henry


cheol
Reply all
Reply to author
Forward
0 new messages