CMock + Ceedling - how to mock methods imported from external library e.g. <nfc/nfc.h>

1.2k views Asked by At

I'm using ceedling + Cmock to unit test my methods. I've been following this guide. In my project, I have a foo.h and foo.c file:

foo.c:

#include <nfc/nfc.h>
#include <stdbool.h>
#include "foo.h"

void do_something_with_nfc() {
   //... other code

   // how might I mock this call?  It is a method of nfc.h
   bool result = nfc_initiator_transceive_bytes(pnd, msg, msg_len, buffer, buffer_len, 0);
}

In my test_foo.c file:

#include "mock_nfc.h" // should this include be <mock_nfc.h>, <nfc/mock_nfc.h> or "nfc/mock_nfc.h"?
#include "foo.h"

void test_do_something_with_nfc() {
    // mocked behavior
    nfc_initiator_transceive_bytes_IgnoreAndReturn(-1);

    do_something_with_nfc();
}

PROBLEM: The error that I'm getting when I run "ceedling" is:

ERROR: Found no file 'nfc.h' in search paths.

libnfc is a library that I installed and is located at /usr/local/lib/libnfc (archive file). I'm extremely new to C development, but I figured CMock needs to be able to find the nfc.h header file in order to generate the mock. If I were simply compiling my project, I would add -lnfc so my next step was to figure out how ceedling/Cmock needed this to be conveyed. To that end, I looked up ways to specify paths through the ceedling project.yml file:

:paths
  :libraries: [/usr/local/lib, /usr/lib, /usr/local/lib/libnfc.a]

Even with the above changes, I still receive an "nfc.h" not found error. Is it more customary to download the referenced library's source (libnfc in this case), and add that source's path to the ceedling project.yml file? Thanks for the assistance.

1

There are 1 answers

2
SamL On

Add following to YML file, if you need to include the path and see it works

:files:
      :include:
        - +:/usr/lib/**

:paths:
  :test:
    - +:test/**
  :source:
    - src/**
    - test/support/**

  :include:
    - include/**   
  :support:
  :libraries: []
:files:
  :include:
    - +:/usr/lib/**

If you are trying to test symbolic funtions try follwing way instread which worked for me. How to mock socket in C