Using NiceMock as an instance variable using GoogleMock

I want to assign NiceMock with the return value of the method. NiceMock is an instance variable.

class TestFileToOsg : public testing::Test { public: NiceMock<MockFileToOsg>* _mockFileToOsg; protected: virtual void SetUp(); }; void TestFileToOsg::SetUp() { _mockFileToOsg = FixtureFileToOsg::getMockFileToOsgWithValidConfig(); } 

Mounting Method:

 MockFileToOsg* FixtureFileToOsg::getMockFileToOsgWithValidConfig() { MockFileToOsg* fileToOsg = new MockFileToOsg(...); return fileToOsg; } 

The compiler produces the following error:

 error: invalid conversion from 'MockFileToOsg*' to 'testing::NiceMock<MockFileToOsg>*' 

How can I assign an instance variable to the return value of the binding method?

+6
source share
1 answer

In your test class, you should have a pointer to your mockobject:

 class TestFileToOsg : public testing::Test { public: MockFileToOsg* _mockFileToOsg; protected: ... 

Your device should instantiate NiceMock and return a pointer to your mockobject.

 MockFileToOsg* FixtureFileToOsg::getMockFileToOsgWithValidConfig() { MockFileToOsg* fileToOsg = new NiceMock<MockFileToOsg>(...); return fileToOsg; } 

The NiceMock <> comes from mockClass.So NiceMock <> should only be used when creating an instance of MockObject.

+8
source

Source: https://habr.com/ru/post/919017/


All Articles