mercredi 4 mai 2016

How to avoid multiple definitions when defining test doubles

I am new to unit testing and I am trying to define a test double for a function that I am trying to define. It probably doesn't make much difference, but I am using IAR for a Cortex ARM M3 with the included compiler.

In the function that I'm trying to test, I make a call to a function that sends a message over a serial interface. Here is some pseudocode to illustrate what I am trying to accomplish :

#ifdef UNIT_TEST
#include "MessageController.h"
#else
#include "FakeMessageController.h"
#endif

uint8 FunctionToTest(....)
{
  uint8 data[100];
  uint8 error;

  //Prepare data to send here
  SendMessage(data);

  if (error)  return 1;
  else return 0;
}

I have created a copy of the original file that contained the SendMessage function and made mofications to it in another file. I thought that I could change the function that is called through the #ifdef statements, however, that is ot the case.

I get the following error for multiple definitions

Error[Li006]: duplicate definitions for .... ; in "......\Tests\Debug\Obj\MessageController.o", and "....\dvlp\Debug\Tests\Debug\Obj\FakeMessageController.o"

I fully understand the reason why the linker is giving my this error, but is there a workaround to this problem? Or some way of tricking the linker into using the correct function? I can't modify the test function since it is a link to the original file in my project.

Aucun commentaire:

Enregistrer un commentaire