eranpeer / FakeIt

C++ mocking made easy. A simple yet very expressive, headers only library for c++ mocking.
MIT License
1.22k stars 169 forks source link

Verify a method on a parent class was called #211

Open dolfandringa opened 4 years ago

dolfandringa commented 4 years ago

How can I verify that a child class calls a function on the parent class. In the sample below, I have separate unittests for class A. But when testing class B I want to verify that it indeed calls its parent class' test function. Is this possible using fakeit?

#include "fakeit.hpp"
#include <iostream>

class A {
    public:
        virtual void test() {
            a = 5;
        }
        int a;
};

class B: public A {
    public:
        virtual void test() {
            A::test();
            b = 3;
        }
        int b;
};

void test_B_test() {
    B *b = new B();
    fakeit::Mock<B> spy(*b);
    b->test();
    //Verify somehow that A::test was called
    std::cout << b->a << " " << b->b << std::endl;
}

int main() {
    test_B_test();
}
FranckRJ commented 1 year ago

No it's not possible currently and I don't see how we can implement it. The way mocks work is by creating a sub-class that inherit from the class you want to mock, and alter the V-Table to redirect the call to the function we want to be called. When you use a fully qualified name to call your method (in the form Class::method) the call won't go through the V-Table, so we can't redirect it or do anything with it.

I'll keep it open in case anyone have an idea but I guess it will never be implemented.