mardi 27 octobre 2015

Gmock only expect a specific call

I have a C++ class I am attempting to test with GMock. I have the following mock class:

class MyTestMock
{
    public:
        MOCK_METHOD1(myCoolMethod, int(const char*));
        MOCK_METHOD1(myCoolMethod, int(string));
}

Then in my test the following happens:

MyTestMock myMock;
if ( myMock.myCoolMethod("stuff") )
{
    // Stuff I don't care about, don't want to execute
}
if ( myMock.myCoolMethod("moarStuff") )
{
    // More stuff I don't care about, don't want to execute
}
if ( myMock.myCoolMethod("foo") )
{
    // This I care about and want to execute
}

What I would like to do is allow the first two calls to be uninteresting calls that return the default for integers, 0, while I set up a specific expectation on the third call to return 1. This was my attempt to do so:

EXPECT_CALL(myMock, myCoolMethod(TypedEq<const char *>("foo"))).WillOnce(Return(1));

However, this ends up making the test fail. If I do this instead:

EXPECT_CALL(myMock, myCoolMethod(Matcher<const char *>(_))).WillRepeatedly(Return(0));
EXPECT_CALL(myMock, myCoolMethod(TypedEq<const char *>("foo"))).WillOnce(Return(1));

my test passes fine. I am attempting to make unit testing as painless as possible on an old, massive, monolithic-style codebase, so I'd really like to not need the extra magic line to tell it to return the default value. Any ideas on how I can do this with just the one expectation without tanking my test?

Aucun commentaire:

Enregistrer un commentaire