Mock a C function when unit testing with unity

871 views Asked by At

I have a C project with 2 modules - A and B. B has a function that calls a function from A.

int B_func() {
  if (1 == A_func()) {return 1;}
  return 2;
 }

I use unity to test these modules.

TEST(B, test_b) {
  TEST_ASSERT_EQUAL(1, B_func())
}

When I test module B, I want to mock A_func so it will use my implementation and change the return value. Is there a way to do this without changing the source code of module B?

1

There are 1 answers

0
user coder On BEST ANSWER

I ended up using Mimick. https://github.com/diacritic/Mimick

It's a bit cumbersome. I needed to compile my project as a shared object and link it to my tests so my functions will be in the GOT, so it is not ideal, but successfully solves my problem.