I want to assign a NiceMock with the return value of a method. The NiceMock is an instance variable.
class TestFileToOsg : public testing::Test
{
public:
NiceMock<MockFileToOsg>* _mockFileToOsg;
protected:
virtual void SetUp();
};
void TestFileToOsg::SetUp()
{
_mockFileToOsg = FixtureFileToOsg::getMockFileToOsgWithValidConfig();
}
The fixture method is:
MockFileToOsg* FixtureFileToOsg::getMockFileToOsgWithValidConfig()
{
MockFileToOsg* fileToOsg = new MockFileToOsg(...);
return fileToOsg;
}
The compiler throws the following error:
error: invalid conversion from ‘MockFileToOsg*’ to ‘testing::NiceMock<MockFileToOsg>*’
How can I assign the instance variable with the return value of the fixture method?
In your testclass you should only have a pointer to your mockobject:
class TestFileToOsg : public testing::Test
{
public:
MockFileToOsg* _mockFileToOsg;
protected:
...
Your fixture should instantiate a NiceMock and return a pointer to your mockobject.
MockFileToOsg* FixtureFileToOsg::getMockFileToOsgWithValidConfig()
{
MockFileToOsg* fileToOsg = new NiceMock<MockFileToOsg>(...);
return fileToOsg;
}
The NiceMock<> derives from the mockClass.So NiceMock<> must only be used when you instantiate a MockObject.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With