Question

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?

Était-ce utile?

La solution

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

Autres conseils

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;
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top