How to mock a function with the signature `object ()`
-
02-10-2019 - |
Question
I want to mock a method with the declaration A::B X(void)
. The definition is something as follows.
class A {
class B;
virtual B X() = 0;
};
class A::B {
public:
auto_ptr<int> something;
};
My mock class, following this, is quite standard.
class mA : public A
{
public:
MOCK_METHOD0(X, A::B());
};
Compiled, however, this gives me this weirdo error, and I haven't been able to track it down. What is wrong with this?
In member function ‘virtual A::B mA::X()’:
...: error: no matching function for call to ‘A::B::B(A::B)’
...: note: candidates are: A::B::B()
...: A::B::B(A::B&)
Update I have found a failing code sample to demonstrate this.
#include <gmock/gmock.h>
#include <memory>
using std::auto_ptr;
class thing {
public:
class result;
virtual result accessor () = 0;
};
class thing::result {
auto_ptr<int> x; // If this just "int", error goes away.
};
namespace mock {
class thing : ::thing {
public:
MOCK_METHOD0 ( accessor, result() );
};
}
Solution
It is hard to tell without the definitions of A and B. sounds like it is trying to construct a B from a temporary and failing because it can't bind the temporary to a non-const reference.
For example, your copy constructor might be defined as:
class A {
public:
class B {
public:
// This should be const, without good reason to make it otherwise.
B(B&);
};
};
With the fix just making it a const reference.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow