Sorry if the title isn't clear.
I have a virtual base class:
class FileSystemInterface {
public:
virtual ~FileSystemInterface() {};
virtual void save(std::string file_content) = 0;
};
My mock class derives from this:
class MockFile : public FileSystemInterface
{
public:
MOCK_METHOD1(save, void(std::string));
};
Now I have a method I'm testing that takes a unique pointer to FileSystemInterface
:
void my_method(std::unique_ptr<FileSystemInterface>& interface)
My test code is as follows:
std::unique_ptr<FileSystemInterface> mockFile(new MockFile);
EXPECT_CALL(*mockFile, save(_));
my_method(mockFile);
The problem is that EXPECT_CALL
gives me an error class "FileSystemInterface" has no member "gmock_save"
Presumably that is because my mock is a pointer to the base class, but if I change my test as follows:
std::unique_ptr<MockFile> mockFile(new MockFile);
EXPECT_CALL(*mockFile, save(_));
my_method(mockFile);
Then EXPECT_CALL
is fine but now my_method
complains: a reference of type "std::unique_ptr<FileSystemInterface, std::default_delete<FileSystemInterface>> &" (not const-qualified) cannot be initialized with a value of type "std::unique_ptr<MockFile, std::default_delete<MockFile>>"
How can I get around this conflict and create my test? Thank you.
UPDATE
As is pointed out in Alexey Guseynov answer, my method does take ownership of the pointer, so the thing to do was change my method to
void my_method(std::unique_ptr<FileSystemInterface> interface);
and run my test like this:
std::unique_ptr<MockFile> mockFile(new MockFile);
EXPECT_CALL(*mockFile, save(_));
my_method(std::move(mockFile));
std::unique_ptr<MockFile> mockFile(new MockFile);
. But then again, I have no experience with mocks in C++... – Threedimensional