mercredi 17 mai 2017

gmock pointer to base class has no member gmock_

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.

1 commentaire: