Mocking function calls

Sometimes it is not of any interest for a test, wheither the unit makes a call to a particular function or not. By the use of ALLOW macro, shown in sample9 below,2.1a test can indicate to the framework, that function calls to this function is valid at all times during the test.

  void test_addition(void) {
    ALLOW ( invalid );
    EXPECT_MOCK ( add, check_add , EXPECTED_SUM);
    VALIDATE(calculate(3,7,'+'), EXPECTED_SUM);
  }

Functions that are present in the unit and not listed in a EXPECT or EXPECT_MOCK are automatically ignored by the framework. In those cases the framework will not interfere and it will simply transfer control to the function.

If the function given to ALLOW is not present in the unit, the framework will return 0 to the unit whenever the function is called.

With the use of the optional second parameter the default mock can be instructed to return any value to the unit. sample10 shown below2.1illustrates this, by making the default mock for invalid return TRUE at all times during the test.

  void test_addition(void) {
    ALLOW   ( invalid, TRUE );
    VALIDATE(calculate(3,7,'+'), ERROR);
  }

It is also possible to replace the default mock entirely for the duration of the test. By the use of ALLOW_MOCK as shown in sample11 below,2.1the function invalid will be mocked by function mock_invalid. The mock function must have the same function prototype is the function is mocks.

  int mock_invalid(int parameter) {
    return FALSE;
  }

  int check_add(int left_operand, int right_operand) {
    VALIDATE(left_operand,3);
    VALIDATE(right_operand,7);
    return MOCK_VALUE;
  }

  void test_addition(void) {
    ALLOW_MOCK  ( invalid, mock_invalid );
    EXPECT_MOCK ( add, check_add , EXPECTED_SUM);
    VALIDATE(calculate(3,7,'+'), EXPECTED_SUM);
  }

testape

News

The latest headlines from Testape.com

TestApe Release 1171 available, Aug 20th 2014

It has been a long time since last official release and the list of bugfixes, features and supported platforms accumulating in the beta has grown substantially. I am happy to annouce that a new release is ready.

more

TestApe beta version available, Jul 21th 2014

New beta version is now available for download.

more

TestApe Release 880 available, Dec 3rd 2011

New is this release are support for floating point validations and function mocking. Also, MinGW has been added to the list of supported platforms.

more

Forum change, Mar 27th 2011

TestApe forum is now hosted on Proboards. Support questions can be posted here or send directly on email. Due to ...

more

TestApe beta release available, Sep 27th 2011

TestApe can now be used with MinGW GCC on windows. Also supported in this beta are floating point types in validations or when mocking functions

more

IPad update for WebTTY, May 15th 2011

A small fix for webtty scripts, to allow the usage from Apple IPads. Tab on textarea to bring up IPad keyboard - you may have to scroll webpage beneath keyboard, in order to actually see what you're typing.

TestApe Release 791 available, Apr 2nd 2010

This release contains a new flexible mocking system with default mocks automatically generated for unresolved functions. Installation packages are available for GCC/Linux, GCC/CygWin as well Visual Studio 2009/Windows XP or Vista.

more