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

Not working with local classes? #326

Open qqnnhhdmp opened 5 months ago

qqnnhhdmp commented 5 months ago
{ // In block scope:
  struct Callbacks {
      virtual ~Callbacks() = default;
      virtual auto on_ball_pocketed(nbs::eightball::BallId ball_id_) -> void = 0;
  };

  using namespace fakeit;

  Mock<Callbacks> mock_callbacks;
  When(Method(mock_callbacks, on_ball_pocketed)).Return();

  pool.on_ball_pocketed = [&mock_callbacks](auto &&_1) {
      return mock_callbacks.get().on_ball_pocketed(_1);
  };

  Verify(Method(mock_callbacks, on_ball_pocketed)).Exactly(1);
}

Error: error C3640: “CATCH2_INTERNAL_TEST_22::Callbacks::[thunk]: cdecl `void cdecl CATCH2_INTERNAL_TEST_22(void)'::2'::Callbacks::vcall'{8,{flat}}' }'”: 局部类的引用成员函数或虚拟成员函数必须进行定义

Translation of above chinese:

Reference member function or virtual member function of local class must be defined.

FranckRJ commented 5 months ago

https://learn.microsoft.com/en-us/cpp/error-messages/compiler-errors-2/compiler-error-c3640?view=msvc-170

It looks like it's a limitation of MSVC (or C++ itself, I don't know). You can provide an empty definition for the function and it should work I guess.