对构造函数和析构函数的未定义引用

时间:2014-08-21 06:59:00

标签: c++ unit-testing googletest googlemock

我正在尝试编写单元测试usig google testgoogle mock但我没有遇到什么困难。我收到错误(你可以在下面看到)我无法修复。

DummyUT.o: In function `~MockApplicationClass':
srctest/../mocks/AppMock.h:15: undefined reference to `Application::~Application()'
srctest/../mocks/AppMock.h:15: undefined reference to `Application::~Application()'
DummyUT.o: In function `MockApplicationClass':
srctest/../mocks/AppMock.h:15: undefined reference to `Application::Application()'
srctest/../mocks/AppMock.h:15: undefined reference to `Application::~Application()'
collect2: ld returned 1 exit status
make: *** [TestApp] Error 1

这是我的AppMock.h文件:

#ifndef APPMOCK_H_
#define APPMOCK_H_
#include "../../app.hxx"
#include "gmock/gmock.h"
class MockApplicationClass: public Application
{
  public:
    MOCK_CONST_METHOD2(Reset, void(int i, const char* chr));
};
#endif /* APPMOCK_H_ */

这里是app.hxx文件:

class Application {
   public:
      Application();
      ~Application();
      int Reset(int i, const char* chr)
}

Conctructor和析构函数在app.cxx文件中定义。析构函数很简单:

//Deconstruction method
Application::~Application() {
    // release all resources before exit

} // end of ~Application()

DummyUT文件:

#include <sys/types.h>
#include "../DummyClass.h" //path to code being tested
#include "../../app.hxx"
#include "../../testClass.h"
#include "../mocks/dummyMock.h" //path to mock
#include "../mocks/AppMock.h"
#include "../mocks/testMock.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"

using ::testing::AllOf;
using ::testing::Ge;
using ::testing::Le;

TEST(Test, testaplikacji)
{
    const char* a="abc";
    MockApplicationClass theClass;
}
int main(int argc, char** argv)
{
  ::testing::InitGoogleTest(&argc, argv);
  return RUN_ALL_TESTS();
}

并制作文件:

CXX=g++

GTEST_DIR = ../../../UTframework/gmock-1.7.0/gtest
GMOCK_DIR = ../../../UTframework/gmock-1.7.0/


EXXX_PATH_PRE = /home/XXX/dev/exxx

# flags
CPPFLAGS += -isystem $(GTEST_DIR)/include -isystem $(GMOCK_DIR)/include 
CXXFLAGS += -g -Wall -Wextra -pthread
GMOCK_LIB = $(GMOCK_DIR)/make
# All tests produced by this Makefile.  Remember to add new tests you
TESTS = TestApp

CPPFLAGS += -isystem /home/XXX/dev/exxx/target/dataserver/include
CPPFLAGS += -isystem ../../Conflib/include
CPPFLAGS += -isystem /home/XXX/dev/exxx/target/XX/include
# All Google Test headers.  Usually you shouldn't change this
# definition.
GTEST_HEADERS = $(GTEST_DIR)/include/gtest/*.h \
    $(GTEST_DIR)/include/gtest/internal/*.h

# All Google Mock headers. Note that all Google Test headers are
# included here too, as they are #included by Google Mock headers.
# Usually you shouldn't change this definition.
GMOCK_HEADERS = $(GMOCK_DIR)/include/gmock/*.h \
    $(GMOCK_DIR)/include/gmock/internal/*.h \
    $(GTEST_HEADERS)

all: $(TESTS)

clean:
rm -f $(TESTS) *.o

TestCIPApp: DummyUT.o DummyClass.o testClass.o
    $(CXX) $(CPPFLAGS) $(CXXFLAGS)  $^ -L$(GMOCK_LIB) -lgmock_main -o $@

DummyUT.o: srctest/DummyUT.cpp
    $(CXX) $(CPPFLAGS) $(CXXFLAGS) -c $^

DummyClass.o:   DummyClass.cpp
    $(CXX) $(CPPFLAGS) $(CXXFLAGS) -c $^ -o $@
copy:
    cp $(TESTS) ../../../UTBuildAll

1 个答案:

答案 0 :(得分:1)

如前所述,您必须为App添加另一个目标:

app.o:   app.cpp

TestCIPApp必须依赖于此:

TestCIPApp: DummyUT.o DummyClass.o testClass.o app.o

现在,对象文件也在那里,可以找到符号。