eranpeer / FakeIt

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

Crash on Mock with Method which returns a reference #135

Closed DavidMeiners closed 6 years ago

DavidMeiners commented 6 years ago

Following code will crash, is there something special i need to do with methods which return refs ?

class Test
{
public:
    virtual const double& Update() = 0;
};

Mock<Test> mock2;

When(Method(mock2, Update)).AlwaysDo([]()
{
    static const double test = 909.0;
    return test;
});

auto res = mock2.get().Update();
DavidMeiners commented 6 years ago

This version is working(lambda return type defined manually):

class Test
{
public:
    virtual const double& Update() = 0;
};

Mock<Test> mock2;

When(Method(mock2, Update)).AlwaysDo([]() -> const double&
{
    static const double test = 909.0;
    return test;
});

auto res = mock2.get().Update();