Skip to content

Wrong error message if mock is used #1775

Open

Description

@f-zl
// code_under_test.c
#include "code_under_test.h" // declares `int function_under_test(int x, int y);`
#include "code_dependency.h" // declares `int dependency_function(int);`
int function_under_test(int x, int y) { return dependency_function(x) + y; }
// test.cpp
#include "CppUTest/TestHarness.h"
#include "CppUTestExt/MockSupport.h"
extern "C" {
#include "code_dependency.h"
#include "code_under_test.h"
}
TEST_GROUP(OneTestGroup){
  void teardown() {
	mock().clear();
  }
};
// mock function
int dependency_function(int arg) {
  return mock()
      .actualCall("dependency_function")
      .withParameter("arg", arg)
      .returnIntValueOrDefault(arg);
}
TEST(OneTestGroup, OneTest) {
  mock().expectOneCall("dependency_function").withParameter("arg", 2);
  // the error message is Mock Failure: Unexpected additional (2nd) call to function: dependency_function
  // the error message should be CHECK_EQUAL error, expect=6, actual=5
  CHECK_EQUAL(6, function_under_test(2, 3));
  mock().checkExpectations();
}

#include "CppUTest/CommandLineTestRunner.h"
int main(int ac, char **av) {
  return CommandLineTestRunner::RunAllTests(ac, av);
}

The test OneTest calls dependency_function only once by calling function_under_test. The expect value and the actual value is different. The error message is Mock Failure: Unexpected additional (2nd) call to function: dependency_function. It should be CHECK_EQUAL error, expect=6, actual=5.

Maybe the test macro should evaluate the argument only once to avoid repeating the side effect of the argument function. For example:

#define CHECK_EQUAL_LOCATION(expected, actual, text, file, line)\
  decltype(expected) expectedValue = expected;\
  decltype(actual) actualValue = actual;\
  do { if ((expectedValue) != (actualValue)) { \
  ...

Activity

  1. fgrie commented on May 6, 2024

    @fgrie

    The current solution (which unfortunately doesn't cover your problem) was added in 97576dc.

    I would guess that the reasoning behind this implementation was the fact that decltype is not supported in language standards prior C++11. Maybe @basvodde knows more on the background of this decision back then.

    In fact, I'd say, there's one central question: does CppUTest want to stay compatible with older C++-standards (if it's still compatible with older standards, at all), or does it address modern C++? I don't know if there is any statement, which language standard is officially supported/targeted?

    Edit: I quickly checked it, CppUTest is still C++98-compatible (tested using w64devkit 1.17.0, which includes GCC )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions