pBouillon
pBouillon

Reputation: 363

Verify program calls with gtest & gmock

I'm using google mock & google test and I have an object such as:

  class Foo {
    public :
        Foo(){}

        virtual void method(int arg) {
            int var = a(arg) ;
            if (var<5){
                b() ;
            }
        }

        virtual int a(int arg){
            // do stuff
        }

        virtual int b(){
            // do stuff
        }
}

I want to check that a() is called one time and b() is not whenever a failed ( = return a value <5) So I wrote something like:

MockFoo mock ;

mock.method(badArg);

EXPECT_CALL(
    mock,
    a
)
.Times(1) ;

EXPECT_CALL(
    mock,
    a
)
.Times(0) ;

But gtest tell me that none of those methods are called, what should I use?

Thanks for your explainations

Upvotes: 0

Views: 1420

Answers (1)

Uduse
Uduse

Reputation: 1581

To use gmock properly, you need to set up your expectation before you run the real thing. This way, gmock knows what's coming and will be able to analyze if the real stuff really meets requirements. Otherwise, if you call the function before you set up your expectations, it's like asking a catcher to get into his standby position after you throw the ball. The catcher off course will miss.

In your case, you want something like:

MockFoo mock ;

// Catchers ready!
EXPECT_CALL(mock, a)
    .Times(1) ;
EXPECT_CALL(mock, b)
    .Times(0) ;

// Here's the ball!!
mock.method(watermelon);

Upvotes: 2

Related Questions