CppUTestでサンプルのMockを使おうとして嵌った箇所の対応方法を本記事に残そうと思う。
#サンプルのMockでビルドエラー
下記サンプルコードをmakeしたところ・・・・
#include "CppUTest/TestHarness.h"
#include "CppUTestExt/MockSupport.h"
TEST_GROUP(MockDocumentation)
{
void teardown()
{
mock().clear();
}
};
void productionCode()
{
mock().actualCall("productionCode");
}
TEST(MockDocumentation, SimpleScenario)
{
mock().expectOneCall("productionCode");
productionCode();
mock().checkExpectations();
}
こんなエラーが出てきた。
testMockBuildTimeTest.cpp:16: undefined reference to `mock(SimpleString const&, MockFailureReporter*)'
testMockBuildTimeTest.cpp:21: undefined reference to `mock(SimpleString const&, MockFailureReporter*)'
testMockBuildTimeTest.cpp:23: undefined reference to `mock(SimpleString const&, MockFailureReporter*)'
・・・・・
#原因
cpputest-*.*\build内のMakefileWorker.mkの下記定義がデフォルトNとなっているために、Mock機能が使えないみたい。
# No extentions is default
ifndef CPPUTEST_USE_EXTENSIONS
CPPUTEST_USE_EXTENSIONS = N
endif
#解決策
プロジェクト内のMakefileに上記定義をYにした一文を追加する。
#Set this to @ to keep the makefile quiet
SILENCE = @
#---- Outputs ----#
COMPONENT_NAME = Project
#Set this to @ to keep the makefile quiet
SILENCE = @
#--- Inputs ----#
PROJECT_HOME_DIR = .
ifeq "$(CPPUTEST_HOME)" ""
CPPUTEST_HOME = ../CppUTest
endif
CPP_PLATFORM = Gcc
SRC_DIRS = \
src\
src/*
# to pick specific files (rather than directories) use this:
SRC_FILES =
TEST_SRC_DIRS = \
tests \
tests/*
MOCKS_SRC_DIRS = \
mocks \
INCLUDE_DIRS =\
.\
include \
include/* \
$(CPPUTEST_HOME)/include/ \
$(CPPUTEST_HOME)/include/Platforms/Gcc\
mocks
CPPUTEST_WARNINGFLAGS = -Wall -Werror -Wswitch-default
CPPUTEST_WARNINGFLAGS += -Wconversion -Wswitch-enum
# CppUMockを使う場合'Y'に
CPPUTEST_USE_EXTENSIONS = Y
include $(CPPUTEST_HOME)/build/MakefileWorker.mk
変更後に再度、makeすると。
compiling AllTests.cpp
compiling testMockBuildTimeTest.cpp
compiling testMockBuildTime.cpp
Building archive lib/libtestMock.a
a - objs/src/util/testMockBuildTime.o
Linking testMock_tests
Running testMock_tests
.
OK (1 tests, 1 ran, 1 checks, 0 ignored, 0 filtered out, 0 ms)
ビルドが成功した。