mardi 21 avril 2015

how to use gmock to check if a member method has been called by another member method

I'm trying to test a class (say, Bar) method using google mock framework. One method, Bar::A() is called inside Bar::B() something like :

void Bar::B() {
    ...
    for (.....) {
        if (....)
            A();
     ......

Now I want to use gmock to verify if Bar::A() has been called 6 times in one Bar::B() call.

I create a mock object and delegate the calls to real object, following this: http://ift.tt/1ICOTSj

I can delegate calls and when debugging I can see that Foo::B() is called when I call MockBar::B(). however, MockBar::A() is not invoked, since Bar::B() will call Bar::A(), but not MockBar::A().

Is there any way to check if A() is invoked by B()? it seems that if I delegate to the real B(), the mock version of A() will not be called. But if I don't delegate to the real function, how can I test the code inside? Or is it not a good idea to test the inside of an interface?

BTW: a slight difference between gmock's example and my own code is that, in their example, the base class and the "real_" object are the same type(Foo). (which implies that their base class is a concrete class). while in mine, since Mock needs virtual methods, my the mock class inherited from an abstract class. And the private member "real_" is a concrete class instance.

Aucun commentaire:

Enregistrer un commentaire