5

I'm just starting out with Cmocka, I've been stuck on this issue for a while now. I have a cmocka project that I'm trying to build. I'm seeing the error when I try to use 'make'.

[ ERROR ] --- No entries for symbol __wrap_i2c_read.

Also I'm seeing an error in the mock_i2c.c file at the line where I call mock()

Could not get value to mock function __wrap_i2c_read.

Right now I'm just trying to mock a true/false value to get it working. So my mock looks like

bool __wrap_i2c_read(void)
{
    return (mock());
}

I checked that in my test I'm calling will_return(__wrap_i2c_read, true);

In my Makefile I have LDFLAGS += -Wl,--wrap=i2c_read I have cmocka.hincluded in the mock_i2c.c file.

This doesn't seem to be a problem specifically for this mock function because if I don't use it, I get the same error for other mock functions in that file. I'm not sure what other info is needed, please let me know. Anyone know what this means/seen this before?

Thanks.

Edit: So I think I've figured out why I'm getting this error. I have a for loop in the function I'm testing. The mocked functions are called from this function. Once I remove the loop, the error goes away. Might this have something to do with how/when the will_return queues the mock values? And the for loop is getting in the way?

Edit2: Ok, so it seems I was just not queuing enough mock values.

User_MK
  • 63
  • 1
  • 6

1 Answers1

3

Your edit 2 is the clue!

You must call will_return as often as mock is called.

For every call of your FUT (function under test) mock() is called. So you need to use will_return before you call your FUT as often as you are going to call your FUT. will_return puts one element onto a stack. mock() will take one element from this stack. So calling will_return one time and mock() two times will cause an underflow which ends up in your mentioned cmocka error.

Maybe you would like to use will_return_always or will_return_count.

Misha Brukman
  • 12,938
  • 4
  • 61
  • 78
eDeviser
  • 1,605
  • 2
  • 17
  • 44