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

Successful verification results are not reported when using Catch #119

Open fgsalomon opened 6 years ago

fgsalomon commented 6 years ago

When using Fakeit along Catch Verify successes are not reported.

#define CATCH_CONFIG_MAIN
#include "catch.hpp"
#include "fakeit.hpp"

using namespace fakeit;

struct SomeInterface {
    virtual void foo(int) = 0;
    virtual int bar(int,int) = 0;
    virtual ~SomeInterface() = 0;
};

TEST_CASE("FakeItTest"){

    Mock<SomeInterface> mock;  
    SomeInterface & i = mock.get();

    When(Method(mock,foo)).AlwaysReturn(); 
    When(Method(mock,bar)).AlwaysReturn(0); 

    SECTION("Section"){
        CHECK(i.bar(1,1) == 0);
        Verify(Method(mock,bar)).AtLeast(1);
    }
}

If I execute this test only 1 assertion is reported:

===============================================================================
All tests passed (1 assertion in 1 test case)

But if I change the verification to "AtLeast(2)" 2 assertions are reported:

Verification error
  Expected pattern: mock.bar( Any arguments )
  Expected matches: at least 2
  Actual matches  : 1
  Actual sequence : total of 1 actual invocations:
    mock.bar(1, 1)

===============================================================================
test cases: 1 | 1 failed
assertions: 2 | 1 passed | 1 failed

Catch version: 2.0.1 Generated: 2017-11-03 11:53:39.642003 FakeIt: Generated: 2017-11-05 20:30:40.182814

jlkalberer commented 6 years ago

I tried hacking on the library to fix this but couldn't make it work. I'd love for this to get resolved.

jlkalberer commented 6 years ago

@fgsalomon - I figured out a hack to make it work. You need to make an edit like this:

      Terminator(smart_ptr<SequenceVerificationExpectation> expectationPtr) : _expectationPtr(expectationPtr) {
        bool result = toBool();
        if (result) {
          REQUIRE(true);
        }
      };
fgsalomon commented 6 years ago

@jlkalberer thanks! I don't know if you had seen my previous issue, let's hope Eran finds time for fixing it :)

jlkalberer commented 6 years ago

Yeah, my workaround here makes things work without having to wrap all the calls with CHECK. I'd much rather have a real fix than a band-aid though. What I've done is really, really hacky.

sidwarkd commented 6 years ago

+1 for this issue. Would be nice to see Verify show success in the Catch output. I confirm the same behavior @fgsalomon is reporting on Catch 2.2.2 and FakeIt single header file for Catch from commit 6dc9613.

fidelski commented 2 years ago

This is still the case in 2021, would be glad to see this solved.