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

Argument Matching | String references #245

Closed klatzklatz closed 3 years ago

klatzklatz commented 3 years ago

Hi everyone and thank you for your work!

I have just started using FakeIt and I find this behavior misleading.

struct SomeInterface {
    virtual bool h_str(std::string const& str) = 0;
};

int main() {
    Mock<SomeInterface> mock;
    auto& mock_inst = mock.get();
    Fake(Method(mock, h_str));
    const std::string DUMMY_STR = "a";
    {
        auto b = DUMMY_STR; // test fails
        //auto& b = DUMMY_STR; //With reference test passes
        mock_inst.h_str(b);
    }
    Verify(Method(mock, h_str).Using(DUMMY_STR));
    return 0;
}

I assume FakeIt matches actual references and not the content of strings. However, in most cases we are interested in value and not in the reference (For example, the call was done in the function with copied or moved parameters). I would not say it's a bug, but I would rather expect another behavior and I guess it relates to #234.

Regards, Dmitrii

FranckRJ commented 3 years ago

It's a known limitation : https://github.com/eranpeer/FakeIt/wiki/Dangers-of-checking-arguments-passed-by-reference

No, it won't be fixed with the strcmp matcher.

I don't know how it can be fixed, I've never looked at it.

The workaround I use sometimes is to mock the method with "Do", and manually store the parameter somewhere.

-------- Message d'origine -------- Le 8 juin 2021 à 13:57, Dmitry a écrit :

Hi everyone and thank you for your work!

I have just started using FakeIt and I find this behavior misleading.

struct SomeInterface { virtual bool h_str(std::string const& str) = 0; };

int main() { Mock mock; auto& mock_inst = mock.get(); Fake(Method(mock, h_str)); const std::string DUMMY_STR = "a"; { auto b = DUMMY_STR; // test fails //auto& b = DUMMY_STR; //With reference test passes mock_inst.h_str(b); } Verify(Method(mock, h_str).Using(DUMMY_STR)); return 0; }

I assume FakeIt matches actual references and not the content of strings. However, in most cases we are interested in value and not in the reference (For example, the call was done in the function with copied or moved parameters). I would not say it's a bug, but I would rather expect another behavior and I guess it relates to #234.

Regards, Dmitrii

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub, or unsubscribe.

klatzklatz commented 3 years ago

Oh, okay, thank you!

And sorry for disturbing, have missed that wiki page :(

Have a nice day!

FranckRJ commented 3 years ago

No problem.

Thanks, you too.

-------- Message d'origine -------- Le 8 juin 2021 à 15:43, Dmitry a écrit :

Oh, okay, thank you!

And sorry for disturbing, have missed that wiki page :(

Have a nice day!

— You are receiving this because you commented. Reply to this email directly, view it on GitHub, or unsubscribe.