FLar
FLar

Reputation: 13

Google Test/Mock test fail if expected call is over-saturated

How can I make google test fail if google mocked method is called more than expected times?

Here is the example:

class MockIO : iIO
{
    MOCK_METHOD1(IO_Read, void (uint8_t));
};

TEST(case, test)
{
    MockIO io;
    EXPECT_CALL(io, IO_Read(0x01)).Times(10);
    for (i=0; i<20; i++)
        io.IO_Read(0x01);
}

As I understand I should try

EXPECT_CALL(io, IO_Read(0x01)).Times(10).Throw(exception);

But in embedded projects exceptions not used.

Any ideas?

Upvotes: 1

Views: 4132

Answers (2)

Jens Ehrlich
Jens Ehrlich

Reputation: 873

I think you should use strict mocks for your test.

TEST(case, test){
StrictMock<MockIO> io;
EXPECT_CALL(io, IO_Read(0x01)).Times(10);

When using a Strictmock, not expected calls cause a testfailure.

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

Upvotes: 3

BЈовић
BЈовић

Reputation: 64263

But in embedded projects exceptions not used.

It doesn't matter, because you should not build your unit test to be run on the embedded platform, but for your PC.

Setting the expectations can be reduced to this :

EXPECT_CALL(io, IO_Read(0x01)).Times( AtLeast( 10 ) );

Failing to satisfy the condition is going to throw an exception.

To enable googlemock library to throw exceptions on failed expectations :

::testing::GTEST_FLAG(throw_on_failure) = true;

Upvotes: 1

Related Questions