-1

Is there any way to mock static global variable to use in test function? Or any workaround for that?

Below example of such situation:

static zsock_t *publish_logs =  NULL;

int btak_log_message_reactor(zloop_t *loop, zsock_t *reader, void *arg) {
    struct btak_log_message *message;
    size_t message_size;

    if(zsock_recv(reader, "b", &message, &message_size) == 0) {
        push_log_message(message);
        if(publish_logs)
            publish_log_message(message, publish_logs);
        free(message);
    }

    return 0;
}
PaulWebbster
  • 1,480
  • 2
  • 14
  • 27
  • I'm not familiar with CppUTest, but it's not possible to just create an accessor and return the mock variable when needed? – dfranca Apr 13 '16 at 10:35
  • Yes, I've think about that. But in this way I produce not really usable code so I was wondering if there is possible to mock just the global variable. – PaulWebbster Apr 13 '16 at 10:43

1 Answers1

2

We often forget about the linker. We can use a different object file to mock an interface. This mock-obj would contain a different definition of your global.

But current unit test frameworks only work on code level... So some build-fu will be needed to add mock objects to the test build.

xtofl
  • 40,723
  • 12
  • 105
  • 192