Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 使用CoTaskMemFree时,Google模拟测试返回无结果_C++_Unit Testing_Winapi_Googlemock - Fatal编程技术网

C++ 使用CoTaskMemFree时,Google模拟测试返回无结果

C++ 使用CoTaskMemFree时,Google模拟测试返回无结果,c++,unit-testing,winapi,googlemock,C++,Unit Testing,Winapi,Googlemock,我在一个类中有一个使用CoTaskMemFree清理内存的方法: HRESULT A::doStuff() { PWSTR protectedPassword = L""; HRESULT hr = helper->ProtectMyPassword(L"Secret", &protectedPassword); LOG("Before CoTaskMemFree"); CoTaskMemFree(protectedPassword); LO

我在一个类中有一个使用CoTaskMemFree清理内存的方法:

HRESULT A::doStuff() {
    PWSTR protectedPassword = L"";
    HRESULT hr = helper->ProtectMyPassword(L"Secret", &protectedPassword);
    LOG("Before CoTaskMemFree");
    CoTaskMemFree(protectedPassword);
    LOG("After CoTaskMemFree");
    return hr;
}
我想测试是否调用了ProtectMyPassword方法,因此我模拟了我的助手类并编写了以下测试:

TEST(TestA, MyTest)
{
    // arrange
    NiceMock<HelperMock> helperMock;
    A classUnderTest = A(&helperMock);
    PWSTR password = L"SuperSecretPwd";
    EXPECT_CALL(helperMock, ProtectMyPassword (_, _)).WillByDefault(DoAll(SetArgPointee<2>(password), Return(1)));

    // act
    HRESULT hr = classUnderTest.doStuff();

    // assert
    EXPECT_TRUE(SUCCEEDED(hr));
}
在我的日志中,我看到CoTaskMemFree之前,但不是CoTaskMemFree之后。我确信这是一件我看不到的小事,但我就是无法解决它。 有人能解释这种行为吗?

CoTaskMemFree用于释放用CoTaskMemAlloc或CoTaskMemRealloc分配的内存。如果不是这样,它将失败

您的模拟生成的指针不符合此条件

PWSTR password = L"SuperSecretPwd";

因此出现了运行时错误。为了解决这个问题,您需要从COM堆中分配模拟

实际上,它们正在传递一个静态字符串:PWSTR password=LSuperSecretPwd;。EXPECT_CALL宏似乎合成了一个函数调用,该函数调用只将out参数设置为该指针并返回1。
PWSTR password = L"SuperSecretPwd";