Google Mock - 模拟两个依赖接口

时间:2017-01-26 12:28:41

标签: c++ unit-testing googlemock object-slicing

我有两个我喜欢模拟的界面。不幸的是,一个取决于另一个,反之亦然。 ICredentialProviderCredentialEvents有一个方法,期望ICredentialProviderCredential作为参数。 ICredentialProviderCredential有一种方法需要ICredentialProviderCredentialEvents作为参数:

class MockCredentialEvents : public ICredentialProviderCredentialEvents
{
    MOCK_METHOD0(Release, ULONG());
    MOCK_METHOD0(AddRef, ULONG());
    MOCK_METHOD2(QueryInterface, HRESULT(IID, void**));
    MOCK_METHOD3(SetFieldString, HRESULT(ICredentialProviderCredential creds, DWORD dwFieldID, LPCWSTR str));
    MOCK_METHOD4(SetFieldCheckbox, HRESULT(ICredentialProviderCredential creds, DWORD dwFieldID, BOOL selected, LPCWSTR str));
    MOCK_METHOD3(SetFieldBitmap, HRESULT(ICredentialProviderCredential creds, DWORD dwFieldID, HBITMAP bitmap));
    MOCK_METHOD3(SetFieldComboBoxSelectedItem, HRESULT(ICredentialProviderCredential creds, DWORD dwFieldID, DWORD value));
    MOCK_METHOD3(DeleteFieldComboBoxItem, HRESULT(ICredentialProviderCredential creds, DWORD dwFieldID, DWORD value));
    MOCK_METHOD3(AppendFieldComboBoxItem, HRESULT(ICredentialProviderCredential creds, DWORD dwFieldID, LPCWSTR str));
    MOCK_METHOD3(SetFieldSubmitButton, HRESULT(ICredentialProviderCredential creds, DWORD dwFieldID, DWORD value));
    MOCK_METHOD1(OnCreatingWindow, HRESULT(HWND));
    MOCK_METHOD3(SetFieldState, HRESULT(ICredentialProviderCredential creds, DWORD dwFieldID, CREDENTIAL_PROVIDER_FIELD_STATE cpfs));
    MOCK_METHOD3(SetFieldInactiveState, HRESULT(ICredentialProviderCredential creds, DWORD dwFieldID, CREDENTIAL_PROVIDER_FIELD_INTERACTIVE_STATE cpfis));
};

class MockCredential : public ICredentialProviderCredential
{
    MOCK_METHOD0(Release, ULONG());
    MOCK_METHOD0(AddRef, ULONG());
    MOCK_METHOD2(QueryInterface, HRESULT(IID iid, void** object));
    MOCK_METHOD1(Advise, HRESULT(ICredentialProviderCredentialEvents events));
    MOCK_METHOD0(UnAdvise, HRESULT());
    MOCK_METHOD1(SetSelected, HRESULT(BOOL selected));
    MOCK_METHOD0(SetDeselected, HRESULT());
    MOCK_METHOD3(GetFieldState, HRESULT(DWORD dwFieldID, CREDENTIAL_PROVIDER_FIELD_STATE cpfs, CREDENTIAL_PROVIDER_FIELD_INTERACTIVE_STATE cpfis));
    MOCK_METHOD2(GetStringValue, HRESULT(DWORD dwFieldID, PWSTR str));
    MOCK_METHOD2(GetBitmapValue, HRESULT(DWORD dwFieldID, HBITMAP bitmap));
    MOCK_METHOD3(GetCheckboxValue, HRESULT(DWORD dwFieldID, BOOL selected, PWSTR str));
    MOCK_METHOD3(GetComboBoxValueCount, HRESULT(DWORD dwFieldID, DWORD val1, DWORD val2));
    MOCK_METHOD3(GetComboBoxValueAt, HRESULT(DWORD dwFieldID, DWORD, PWSTR str));
    MOCK_METHOD2(GetSubmitButtonValue, HRESULT(DWORD dwFieldID, DWORD adjacent));
    MOCK_METHOD2(SetStringValue, HRESULT(DWORD dwFieldID, PCWSTR str));
    MOCK_METHOD2(SetCheckboxValue, HRESULT(DWORD dwFieldID, BOOL selected));
    MOCK_METHOD2(SetComboBoxSelectedValue, HRESULT(DWORD dwFieldID, DWORD value));
    MOCK_METHOD1(CommandLinkClicked, HRESULT(DWORD dwFieldID));
    MOCK_METHOD4(GetSerialization, HRESULT(CREDENTIAL_PROVIDER_GET_SERIALIZATION_RESPONSE cpgsr, CREDENTIAL_PROVIDER_CREDENTIAL_SERIALIZATION cpcs, PWSTR str, CREDENTIAL_PROVIDER_STATUS_ICON icon));
    MOCK_METHOD4(ReportResult, HRESULT(NTSTATUS status, NTSTATUS substatus, PWSTR pwstr, CREDENTIAL_PROVIDER_STATUS_ICON icon));
};

问题是所有具有另一个接口作为参数的MOCK_METHODx调用都会触发此错误:

Error: object of abstract class type "*** " is not allowed.

我该怎么做才能避免此错误?

0 个答案:

没有答案