eranpeer / FakeIt

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

Quickstart spy example does not work as expected #307

Open dschulze42 opened 1 year ago

dschulze42 commented 1 year ago
#include <stdio.h>
#include "fakeit.hpp"

using namespace fakeit;

SCENARIO("example", "[]")
{
    GIVEN("example")
    {
        class SomeClass {
        public:
            virtual int func1(int arg) {
                    return arg;
            }
            virtual int func2(int arg) {
                    return arg;
            }
        };

        SomeClass obj;
        Mock<SomeClass> spy(obj);

        When(Method(spy, func1)).AlwaysReturn(10); // Override to return 10
        Spy(Method(spy, func2)); // Spying func2 without changing any behavior

        SomeClass& i = spy.get();
        std::cout << i.func1(1) << std::endl; // will print 10.
        std::cout << i.func2(1) << std::endl; // func2 is not stubbed.
    }
}

That's exactly the example from the quickstart document. When compiled with mingw gcc (v.10.2.0) the output is

10
25163300

Shouldn't the func2 call output 1?

When compiled with mingw clang (v.10.0.0) the output is

10

So the executable crashes. It seem to crash in class RecordedMethodBody::handleMethodInvocation but I don't get a usable exeption/error message and am not good enough at C++ to fully understand what the problem could be.

We are using fakeit v2.3.2

FranckRJ commented 1 year ago

I've tried with clang 14.0.6 and gcc 12.2.0 on Linux and the examples work. The library isn't tested with mingw, maybe it's not compatible with it. I don't have a mingw setup right now so I can't really test, but I'll try to when I can.

malcolmdavey commented 1 year ago

Wondering if it might be related to either of these window problems (mingw is a gcc on windows)

https://github.com/eranpeer/FakeIt/issues/279 https://github.com/eranpeer/FakeIt/issues/212