我想用方法的返回值分配一个NiceMock。 NiceMock是一个实例变量。class TestFileToOsg : public testing::Test{public: NiceMock<MockFileToOsg>* _mockFileToOsg;protected:virtual void SetUp();};void TestFileToOsg::SetUp(){ _mockFileToOsg = FixtureFileToOsg::getMockFileToOsgWithValidConfig();}固定方法是:MockFileToOsg* FixtureFileToOsg::getMockFileToOsgWithValidConfig(){ MockFileToOsg* fileToOsg = new MockFileToOsg(...); return fileToOsg;}编译器将引发以下错误:error: invalid conversion from ‘MockFileToOsg*’ to ‘testing::NiceMock<MockFileToOsg>*’如何为实例变量分配Fixture方法的返回值? 最佳答案 在您的测试类中,您应该只具有一个指向您的模拟对象的指针:class TestFileToOsg : public testing::Test{public: MockFileToOsg* _mockFileToOsg;protected:...您的装置应实例化NiceMock并返回指向您的模拟对象的指针。MockFileToOsg* FixtureFileToOsg::getMockFileToOsgWithValidConfig(){ MockFileToOsg* fileToOsg = new NiceMock<MockFileToOsg>(...); return fileToOsg;}NiceMock 派生自模拟类。因此,仅当您实例化MockObject时,才必须使用NiceMock 。关于c++ - 在Google中将NiceMock用作实例变量,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/11204458/
10-09 06:03