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

Question about `Verify` + `Matching` lambda #80

Closed darynmitchell closed 7 years ago

darynmitchell commented 7 years ago

I've been looking for how to do verification of calls that check more complex argument types/values.

I saw in #68 that @reneme used Verify with Matching:

Verify(Method(mock, bar).Matching([](QString val){return val == "foo";}).Exactly(2_Times):

Does that exist in FakeIt? I've tried to do the same but the Matching lambda doesn't seem to get called... just need to check if I've set it up wrong or that's not actually part of FakeIt.

Context: Using FakeIt 2.0.2 single file header + 'catch'

eranpeer commented 7 years ago

Sure it does, and well documented at the Quickstart under 'Invocation Matching' and 'Argument Matching' sections. Here are some examples of what you can do (form the quickstart):

// Stub foo to return 1 only when argument 'a' is even.auto argument_a_is_even = [](int a){return a%2==0;};When(Method(mock,foo).Matching(argument_a_is_even)).Return(1); // Throw exception only when argument 'a' is negative.auto argument_a_is_negative = [](int a){return a < 0;};When(Method(mock,foo).Matching(argument_a_is_negative)).Throw(exception()); // Stub bar to throw exception only when argument 'a' is bigger than argument 'b'.auto a_is_bigger_than_b = [](int a, int b){return a > b;};When(Method(mock,bar).Matching(a_is_bigger_than_b)).Throw(exception());// Or, with C++14:When(Method(mock,bar).Matching([](auto a, auto b){return a > b;})).Throw(exception());

Here is another example taken from the unittests of fakeit:

Mock mock; When(Method(mock, func).Matching(->bool{return true;})).Return(0); SomeInterface &i = mock.get(); i.func(1); ....

Hope this helps, Eran.

On Sun, Feb 12, 2017 at 4:09 PM, Daryn Mitchell notifications@github.com wrote:

I've been looking for how to do verification of calls that check more complex argument types/values.

I saw in #68 https://github.com/eranpeer/FakeIt/issues/68 that @reneme https://github.com/reneme used Verify with Matching:

Verify(Method(mock, bar).Matching([](QString val){return val == "foo";}).Exactly(2_Times):

Does that exist in FakeIt? I've tried to do the same but the Matching lambda doesn't seem to get called... just need to check if I've set it up wrong or that's not actually part of FakeIt.

Context: Using FakeIt 2.0.2 single file header + 'catch'

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/eranpeer/FakeIt/issues/80, or mute the thread https://github.com/notifications/unsubscribe-auth/ACc8gk7Tscjd9cn3OnY5RCVp_wngnYZiks5rbxKHgaJpZM4L-fvz .

-- Eran 972-52-8387550