Search code examples
c++cunit-testingincludecpputest

Mock().enable and disable are not recognized compiling test group project with cpputest lib


I'm trying to compile my test group project but I have no success since the next compiling error that I can not understand in output:

Console output:

"test_TestHW.c: In member function ‘virtual void TEST_TestHW_TestHW_main_Test::testBody()’:
 test_TestHW.c:617:6: error: request for member ‘enable’ in ‘mock’, which is
 of non-class type ‘MockSupport&(const SimpleString&, MockFailureReporter*)’
 mock.enable();
 ^
  test_TestHW.c:651:6: error: request for member ‘disable’ in ‘mock’, which is of non-class     
  type ‘MockSupport&(const SimpleString&, MockFailureReporter*)’ 
  mock.disable();"

Part of project codes:

Test group code .c file.

  /*******************************************************************************
*   INCLUDES
*******************************************************************************/

#include <CppUTest/CommandLineTestRunner.h>

#include <CppUTest/TestHarness.h> 

#include <CppUTestExt/MockSupport.h>

extern "C"
{
    #include "RFID_Drv.h"
    #include "HAL_AS393x_mock.h"
}

TEST_GROUP (TestHW)
{ 
  protected:    

  public:       
    /* Define data accessible to test group members here */
    void setup()
    {       
         mock().disable();
    }

    void teardown()
    {
        /* Clean up steps are executed after each TEST */           
        mock().checkExpectations();
        mock().clear();
    }   
 };

 TEST(TestHW,TestHW_main_FC_cuenta)
 {

    unsigned char error_val;

    FLAG_Ocupado =0;
    ControlEmi = 150;   /* Valor de frecuencia para probar */
    mock.enable();
    mock().expectOneCall("CapturaTimer").andReturnValue(1000);
    error_val=TestHW();
    CHECK_EQUAL(error_val,FCENTRAL_CUENTA)  /* Entra en el esatdo 2 */
    CHECK_EQUAL(ControlEmi, 150);
    mock.disable();
 }

    .......

    //more test cases here

    .......

int main(int ac, char** av)
{   
     /* Executes all the tests */
     CommandLineTestRunner::RunAllTests(ac,av); 

      /* Returns value */
     return(0);
 }

Includes in mock.c file:

 /*******************************************************************************
 *  INCLUDES                                                                
 *******************************************************************************/

 #include <CppUTest/TestHarness.h>

 #include <CppUTestExt/MockSupport.h>


 extern "C"
 {  
     #include "timer_mock.h"        
 }

 unsigned long CapturaTimer(void)
 {
   mock().actualCall("CapturaTimer");
   return mock().unsignedIntReturnValue();
 }

It seems that enable/disable are not considered and unknown by cpputest. I think it could be a silly thing that I've missed. But now I'm unable to see what one.

I know I'm testing C sources functions within a Cpp test file. Due this I was using extern c instance. I'm surprised because dis/en are not recognized but Mock().expectonecall is recognized (no compiling error).

  • Hence, will be there another way to en/disable mocks at the present case?
  • Can it be seen some error in the way of including cpputest related dependencies, etc? and if it possible how to fix them?

Solution

  • I have found the cause of this error: I forgot "()" in:

     mock.enable();
    

    It must be replaced with:

     mock().enable(); 
    

    So it compiles.