GMock - 使用另一个实现模拟抽象 class

GMock - Mocking an abstract class with another implementation

我正在尝试模拟 IAudioMeterInformation Windows API(我只需要 GetPeakValue() 方法)。

我遇到了一个错误:

Error C2259 'AudioMeterInformationMock': cannot instantiate abstract class

所以我尝试在我的模拟中创建 GetPeakValue() 的自定义实现:

#include <gmock/gmock.h>

class AudioMeterInformationMock : public IAudioMeterInformation
{
public:
   MOCK_METHOD1_WITH_CALLTYPE(STDMETHODCALLTYPE, GetPeakValueMod, HRESULT(float *pfPeak));

   virtual HRESULT STDMETHODCALLTYPE GetPeakValue(float *pfPeak)
   {
    *pfPeak = 1.0;
    GetPeakValueMod(pfPeak);
    return S_OK;
   }
};

我在测试中称它为:

std::unique_ptr<AudioMeterInformationMock> audioMeterMock_ = std::make_unique<AudioMeterInformationMock>();
EXPECT_CALL(*audioMeterMock_, GetPeakValueMod(_))
    .WillOnce(Return(S_OK));

但我仍然收到相同的错误消息:

Error C2259 'AudioMeterInformationMock': cannot instantiate abstract class

知道我做错了什么吗?

谢谢!


更新:

原来我必须 "implement" 所有其他纯虚函数才能使其工作。

    MOCK_METHOD1_WITH_CALLTYPE(STDMETHODCALLTYPE, GetPeakValueMod, HRESULT(float *pfPeak));
    MOCK_METHOD1_WITH_CALLTYPE(STDMETHODCALLTYPE, GetMeteringChannelCount, HRESULT(_Out_  UINT *pnChannelCount));
    MOCK_METHOD2_WITH_CALLTYPE(STDMETHODCALLTYPE, GetChannelsPeakValues, HRESULT(UINT32 u32ChannelCount, float *afPeakValues));
    MOCK_METHOD1_WITH_CALLTYPE(STDMETHODCALLTYPE, QueryHardwareSupport, HRESULT(_Out_  DWORD *pdwHardwareSupportMask));

And IUnknown methods:
    MOCK_METHOD0_WITH_CALLTYPE(STDMETHODCALLTYPE, AddRef, ULONG()); \
    MOCK_METHOD0_WITH_CALLTYPE(STDMETHODCALLTYPE, Release, ULONG()); \
    MOCK_METHOD2_WITH_CALLTYPE(STDMETHODCALLTYPE, QueryInterface, HRESULT(const IID&, void**));

它现在按预期工作。

更新:

原来我必须 "implement" 所有其他纯虚函数才能使其工作。

    MOCK_METHOD1_WITH_CALLTYPE(STDMETHODCALLTYPE, GetPeakValueMod, HRESULT(float *pfPeak));
    MOCK_METHOD1_WITH_CALLTYPE(STDMETHODCALLTYPE, GetMeteringChannelCount, HRESULT(_Out_  UINT *pnChannelCount));
    MOCK_METHOD2_WITH_CALLTYPE(STDMETHODCALLTYPE, GetChannelsPeakValues, HRESULT(UINT32 u32ChannelCount, float *afPeakValues));
    MOCK_METHOD1_WITH_CALLTYPE(STDMETHODCALLTYPE, QueryHardwareSupport, HRESULT(_Out_  DWORD *pdwHardwareSupportMask));

And IUnknown methods:
    MOCK_METHOD0_WITH_CALLTYPE(STDMETHODCALLTYPE, AddRef, ULONG()); \
    MOCK_METHOD0_WITH_CALLTYPE(STDMETHODCALLTYPE, Release, ULONG()); \
    MOCK_METHOD2_WITH_CALLTYPE(STDMETHODCALLTYPE, QueryInterface, HRESULT(const IID&, void**));

它现在按预期工作。