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

Can't move mocks #97

Closed j-mok closed 3 months ago

j-mok commented 7 years ago

Mocks seem to poorly handle moving and copying, causing crashes. It would be convenient to be able to write helper functions that construct mocks, then stub them as needed and move them back to the caller.

vijairaj commented 6 years ago

Like @j-mok said, it would be convenient to have helper methods for constructing and returning mocks but fakeit behaviour seems to be inconsistent. I have given a reduced test case to reproduce the behaviour with Visual studio 2017.

struct IAppliance {
    ~IAppliance() = default;
    virtual void SwitchOn() = 0;
    virtual void SwitchOff() = 0;
};

template<typename T>
fakeit::Mock<T> CreateFakeAppliance()
{
    using namespace fakeit;

    Mock<T> mock;
    When(Method(mock, SwitchOn)).AlwaysReturn();
    When(Method(mock, SwitchOff)).AlwaysReturn();
    return mock;
}

int main()
{
    auto mock = CreateFakeAppliance<IAppliance>();

    // cl /MD /EHsc /O2 test.cpp - works
    // cl /MD /EHsc test.cpp - crashes in the below line (note the missing /O2 flag)
    mock.get().SwitchOn();

    return 0;
}
ranty-fugue commented 5 years ago

I've run into this, too. I think it would make sense to prevent copying and moving mocks until this issue is addressed. How does this sound @eranpeer ?

FranckRJ commented 3 months ago

Fixed by #334, now mocks are movable (they can't be copyable as explained in the PR). It will be released in the next version of FakeIt.