eranpeer / FakeIt

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

Clear invocations on mock, without resetting #83

Closed helmesjo closed 7 years ago

helmesjo commented 7 years ago

Title says it all:

How do I clear the invocations on a mock, without resetting it completely?

Simple example:

TEST(Add_AlreadyHasItem_ListenerIsNotNotified)
{
    auto mock = Mock<MyListener>();
    Fake(Method(mock, OnChange));
    auto container = MyContainer(&mock.get());
    auto item = MyItem();
    container.add(item); // Add once

    mock.clearInvocations(); // <--- This is what I want
    // mock.reset(); // <---- This is NOT what I want, mock might have 10 already faked methods, don't wanna redo it... :)

    container.add(item); // Add twice

    Verify(Method(mock, OnChange)).Never();
}

Scimmed through your code but can't seem to find anything like this.

helmesjo commented 7 years ago

Note: I am aware of the many different ways to get around this (eg. in this case, instead of Never(), use Once()), but that is not a sustainable solution IMO.

helmesjo commented 7 years ago

@eranpeer Any thoughts?

eranpeer commented 7 years ago

I hope I will have time this week to add this functionality. It is a nice enhancement and should be quite simple to implement.

On Apr 10, 2017 15:48, "Fred Helmesjö" notifications@github.com wrote:

@eranpeer https://github.com/eranpeer Any thoughts?

— You are receiving this because you were mentioned.

Reply to this email directly, view it on GitHub https://github.com/eranpeer/FakeIt/issues/83#issuecomment-292939278, or mute the thread https://github.com/notifications/unsubscribe-auth/ACc8gt00jn18Hs_Vz1uI4JsGnXsKSsJcks5ruiUXgaJpZM4Me7-5 .

eranpeer commented 7 years ago

More than week, but finally done. I added a ClearInvocationHistory() method. It will clear the recorded invocations but keep the stubbing. Hope it solves this issue.

helmesjo commented 7 years ago

Great! I've tried a lot of frameworks (mostly C#) and this feature is indeed very handy, especially in this case where one have to re-define the stubbs.

Thank you!