문제

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?

도움이 되었습니까?

해결책

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

다른 팁

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;
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top