eranpeer / FakeIt

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

Unexpected method invocation: unknown() #166

Open trampster opened 5 years ago

trampster commented 5 years ago

When running and I have missed mocking a method I get this message:

fakeit.hpp:1120: Failure Unexpected method invocation: unknown() An unmocked method was invoked. All used virtual methods must be stubbed!

This is really unhelpful, it gives me zero clues as to what method or even type I missed.

not sure why the method is 'unknown()'.

Can the actual method name be printed along with the type?

trampster commented 5 years ago

I'm building with 00 optimisations

andioz commented 5 years ago

I ran into this too, using with catch2.

jaques-sam commented 5 years ago

I got the same message even with a very simple QTest example:

FAIL! : SimpleTest::cleanupTestCase() Unexpected method invocation: unknown() An unmocked method was invoked. All used virtual methods must be stubbed! fakeit.hpp(1121) : failure location FAIL! : SimpleTest::cleanupTestCase() Caught unhandled exception

See here https://gist.github.com/jaques-sam/590a5301dcd3b3f2fd68de4ab110d6b2

conradjones commented 4 years ago

i just started breakpointing on that function and looking at the stack trace to see what i forgot to stub. Not ideal, but i couldn't see an easy way of fixing it.

jaques-sam commented 4 years ago

But that's not a reason to close this issue... Please reopen.

conradjones commented 4 years ago

I would love to re open the issue. Two small problems.

1 I'm not a maintainer so can't open or close issues .

  1. It's not closed
FranckRJ commented 2 years ago

It's technically impossible to show the name of the function if it hasn't been mocked. We could technically show the name of the mock if another function of the mock has been mocked, it would make things a bit easier.

weili-jiang commented 2 years ago

It's technically impossible to show the name of the function if it hasn't been mocked. We could technically show the name of the mock if another function of the mock has been mocked, it would make things a bit easier.

This would be immensely helpful. What about:

fschuh commented 1 year ago

I also often encounter this problem, and the lack of any context sometimes makes it difficult to track down which function is the culprit.

Unknown file(0): FAILED:
explicitly with message:
  Unexpected method invocation: unknown()
    An unmocked method was invoked. All used virtual methods must be stubbed!

Catch2 will at least print out the file and test names, but that's the only information I get.
If it could print any information about the problematic mock at all, it would be a huge improvement.

Looking through the source code, I also just found out about #define FAKEIT_ASSERT_ON_UNEXPECTED_METHOD_INVOCATION.
Enabling this will cause an assert to trigger, which if you have a debugger attached will lead you to the exact line where the mock call failed if you follow your call stack.
Can be a workaround, though it can be awkward to activate, and not something we want to leave on.

fsj commented 1 year ago

The function being called can be determined figured out from the call backtrace when debugging with a breakpoint set at the location where the exception is thrown. So, why not add the top frames of the backtrace to the exception if it is available?

I know, it requires some platform-dependent code, but it shouldn't be too difficult to do for the major platforms and provide an empty implementation with some hook for other platforms (for gtest a GTEST_OS_STACK_TRACEGETTER macro is used to provide this implementation). This would be tremendously useful.