meekrosoft / fff

A testing micro framework for creating function test doubles
Other
787 stars 169 forks source link

fake.callcount is always 0 when reuse a fake across multiple test-suites #118

Open Darwin-Li-001 opened 2 years ago

Darwin-Li-001 commented 2 years ago

Describe the bug A clear and concise description of what the bug is. When reuse a fake across multiple test-suites, Functions with parameters faked as the instruction described, but the callcount is always 0, however the functions without parameters callcount is normal. To Reproduce Steps to reproduce the behavior: 1.Fake a function with parameters as instruction described, with DECLARE_FAKE_VOID_FUNC in a public .h file;

  1. use DEFINE_FAKE_VOID_FUNC in a private .c file
  2. Reference faked function in a Google Test test suit TEST_F;
  3. When I try to use the FUN_fake.callcount, it is always 0;
  4. but if i fake the function directly in the test source file, the callcount is OK.

Expected behavior A clear and concise description of what you expected to happen.

Screenshots image

Compiler, toolset, platform (please complete the following information): gcc.exe (x86_64-posix-seh-rev0, Built by MinGW-W64 project) 8.1.0 vscode V1.72

Darwin-Li-001 commented 2 years ago

@meekrosoft Hi, can you help to give a support? Thank you so much!

Darwin-Li-001 commented 2 years ago

@meekrosoft @michahoiting @zrax @wulfgarpro Anybody here can help this? This is urgent. Thanks!

ff12323 commented 1 year ago

use g++ -E , u will see its a simple Preprocessing Seams, just define a function the same name, then link call it.

the reset just memset value 0. Cannot change called func

void checkSkipList_reset(void)
{
  memset((void *)&checkSkipList_fake, 0, sizeof(checkSkipList_fake) - sizeof(checkSkipList_fake.custom_fake) - sizeof(checkSkipList_fake.custom_fake_seq));
  checkSkipList_fake.custom_fake =
# 6 ".\\fff_test.cpp" 3 4
      __null
# 6 ".\\fff_test.cpp"
      ;
  checkSkipList_fake.custom_fake_seq =
# 6 ".\\fff_test.cpp" 3 4
      __null
# 6 ".\\fff_test.cpp"
      ;
  checkSkipList_fake.arg_history_len = (50u);
};
Darwin-Li-001 commented 1 year ago

et value 0. Cannot change called func

Thank you for your reply!

Can we fix it since you know the root cause?

leonardopsantos commented 1 year ago

This is not a FFF bug.

You're declaring to declare a fake in a public header, you're getting a global struct. It's expected that they'll interfere with each other, so declaring a fake in a global header is generally not a good idea. Your problem is the test code structure that you created.

  1. Use DECLARE_FAKE_VOID_FUNC in your private, GTest (C++) file (extern "C" is required otherwise you get a linker error):
extern "C" {
DECLARE_FAKE_VOID_FUNC(foo, int);
}
  1. Call RESET_FAKE from your test fixture SetUp() method, not from your test (DRY)

I have dozens of tests that use the temple described above and they work perfectly.

See an example here

Note that in that example, I am calling RESET_FAKE from TEST because that's a simple example. It's better if you use a test fixture (like you're doing), so you can call RESET_FAKE from SetUp()