0

I have a piece of code implementing a class that I want to test, lets call it A.cpp and the test test_A.cpp.

The A.cpp file uses some functions from a library that commands a particular device, lets call this libdevice.
In particular, the A class offers a method connect() which internally calls send_conf() function, which is defined inside libdevice. In code:

  • A.cpp file
#include <libdevice.h>;     
public class A{
//private
     public:
     bool connect() {
          int myconf = 3;
          if(send_conf(myconf) == -1) {
              return false;
          }
          return true;
      }
}
  • test_A.cpp
extern "C" {
     MOCK_FUNCTION(send_conf, 1, int(int)); 
};

BOOST_AUTO_TEST_CASE(connect_test) {
    bool ret; 
    A* myA= new A();
    MOCK_EXPECT(send_conf).once().with(3).returns(-1);
    ret = myA->connect();
    BOOST_CHECK_EQUAL(ret, false); 
}

Now, if I use the related MOCK_FUNCTION() macro from mock-turtle library when I compile my test I receive a linker error:

A.cpp: undefined reference to 'send_conf'.

This is due to not linking the library, which at this step is not available. How can I achieve a mock for this set-up in mock-turtle?

Gionata Benelli
  • 357
  • 1
  • 3
  • 20
  • What is the signature for `send_conf()` and how do you use `MOCK_FUNCTION()` exactly? – mat007 Aug 11 '22 at 06:15
  • I added these infos to my original question. The libdevice.so library contains C code, so I used extern "C" to reflect that, it does not work even if i remove the extern "C" keywords – Gionata Benelli Aug 11 '22 at 09:28
  • Are you sure the `send_conf` function doesn’t declare any calling conventions? See http://turtle.sourceforge.net/turtle/reference.html#turtle.reference.creation.function – mat007 Aug 11 '22 at 13:22
  • I saw that section, but it does not show any possible calling convention that i should try. Also, i'm using Mingw32 compiler on windows. – Gionata Benelli Aug 11 '22 at 13:28
  • What does the signature for `send_conf` look like, when you look it up in the header file? – mat007 Aug 12 '22 at 12:52
  • extern "C" { int send_conf(int x); } – Gionata Benelli Aug 12 '22 at 13:38
  • I suppose you could always use a wrapper to redirect e.g. with an `extern "C" { int send_conf(int x) { send_conf_mock(x); }}` and then `MOCK_FUNCTION(send_conf_mock, 1, int(int));`. – mat007 Aug 13 '22 at 06:16

0 Answers0