Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

gmock compilation error (within testing::internal::FunctionMocker) on mock_method

I am getting some weird compilation errors when I try to mock a function. The compiler complains about something with the copy constructor.

Code snippets:

class db_key {
public:
  db_key(void) {}
  explicit db_key(const char* buf) {}
  ~db_key(void) {}
};

class bar_A {
public:
  explicit bar_A(const db_key& key): m_key(key) {}
  virtual ~bar_A(void) {}
  const db_key& dbkey(void) const { return m_key; }

private:
  const db_key m_key;
};

class bar_B: bar_A {
public:
  explicit bar_B(const db_key& key): bar_A(key) {}
  virtual int read(int index) const { return index; }
};

class accessor_c {
public:
  static const char* name(void) { return "general_accessor"; }
  static accessor_c instance(const char* _db_name)
  {
     return accessor_c(_db_name);
  }
  virtual ~accessor_c(void) {}

  const bar_B mem;
  virtual const bar_B& get_bar_B_mem(void) const
  {
    return mem;
  }

protected:
  explicit accessor_c(const char* _db_name):
    mem(db_key("foo")), m_db_name(_db_name) {}

private:
  const char* const m_db_name;
};

inline accessor_c accessor(const char* db_name)
{
  return accessor_c::instance(db_name);
}

The Mock:

class mock_accessor_c : public accessor_c {
public:
  explicit mock_accessor_c(const char* _db_name):
    accessor_c(_db_name) {}
  virtual ~accessor_c(void) {}
  static mock_accessor_c instance(const char* _db_name)
  {
     return mock_accessor_c(_db_name);
  }

  MOCK_CONST_METHOD0(get_bar_B_mem, const bar_B&(void));
};

int main(int argc, char** argv) {
  ::testing::InitGoogleMock(&argc, argv);
  return RUN_ALL_TESTS();
}

I didn't manage to decipher the error message I am getting... here it is:

In file included from /usr/include/gtest/internal/gtest-internal.h:40:0,
                 from /usr/include/gtest/gtest.h:57,
                 from mock.cpp:1:
/usr/include/gmock/gmock-spec-builders.h: In copy constructor ‘testing::internal::FunctionMocker<const bar_B&()>::FunctionMocker(const testing::internal::FunctionMocker<const bar_B&()>&)’:
/usr/include/gmock/gmock-spec-builders.h:1728:3: error: ‘testing::internal::FunctionMockerBase<F>::FunctionMockerBase(const testing::internal::FunctionMockerBase<F>&) [with F = const bar_B&()]’ is private
   GTEST_DISALLOW_COPY_AND_ASSIGN_(FunctionMockerBase);
   ^
In file included from /usr/include/gmock/gmock.h:61:0,
                 from mock.cpp:2:
/usr/include/gmock/gmock-generated-function-mockers.h:61:7: error: within this context
 class FunctionMocker<R()> : public
       ^
mock.cpp: In copy constructor ‘mock_accessor_c::mock_accessor_c(const mock_accessor_c&)’:
mock.cpp:55:7: note: synthesized method ‘testing::internal::FunctionMocker<const bar_B&()>::FunctionMocker(const testing::internal::FunctionMocker<const bar_B&()>&)’ first required here 
 class mock_accessor_c : public accessor_c {
       ^
mock.cpp: In static member function ‘static mock_accessor_c mock_accessor_c::instance(const char*)’:
mock.cpp:62:37: note: synthesized method ‘mock_accessor_c::mock_accessor_c(const mock_accessor_c&)’ first required here 
      return mock_accessor_c(_db_name);
                                     ^

Thanks in advance.

like image 232
Andy Thomas Avatar asked Sep 08 '15 08:09

Andy Thomas


1 Answers

The short answer is that mock objects are not copyable, hence the compiler error about the private constructor. Based on a quick visual inspection of your code, the mock_accessor_c::instance() method is causing this error because it's returning a mock by value, thus making a copy.

I would suggest dropping the mock_accessor_c::instance() method entirely, and instead constructing the mock directly when you need it.

like image 115
Ian Avatar answered Oct 15 '22 00:10

Ian