class CSumWnd : public CBaseWnd
{
private:
bool MethodA()
}
请你能帮如何嘲笑MethodA()
未做虚拟, 我不明白的hi-perf dependency injection
class CSumWnd : public CBaseWnd
{
private:
bool MethodA()
}
请你能帮如何嘲笑MethodA()
未做虚拟, 我不明白的hi-perf dependency injection
这意味着概念你将不得不对你的生产代码进行模板化。使用你的例子:
CSumWind
类定义:
class CSumWnd : public CBaseWnd
{
private:
bool MethodA()
};
嘲笑CSumWnd
类的定义:它必须与嘲笑类CSumWind
测试
class MockCSumWnd : public CBaseWnd
{
private:
MOCK_METHOD(MethodA, bool());
};
生产类。现在它成为在测试中使用CSumWind
类生产代码和MockCSumWnd
类的模板。在生产
template <class CSumWndClass>
class TestedClass {
//...
void useSumWnd(const CSumWndClass &a);
private:
CSumWndClass sumWnd;
};
实例化:
实例化对象的TestedClass <CSumWnd> obj;
测试可执行文件:
TestedClass <MockCSumWnd> testObj;
尝试CppFreeMock和其他一些人提到here。
例子:
string func() {
return "Non mocked.";
}
TEST(HelloWorld, First) {
EXPECT_CALL(*MOCKER(func), MOCK_FUNCTION()).Times(Exactly(1))
.WillOnce(Return("Hello world."));
EXPECT_EQ("Hello world.", func());
}
如果你不想改变现有的代码,这里是VC特定的解决方案++我的工作(https://github.com/mazong1123/injectorpp)。简要步骤如下:
让我们把关键代码放在这里。
检索方法的符号和类的地址。以下是实施的关键思想。完整的源代码是在availiable https://github.com/mazong1123/injectorpp/blob/master/injectorpp/ClassResolver.cpp
// Retrieve class symbol.
if (SymGetTypeFromName(this->m_hProcess, modBase, className.c_str(), classSymbol) == FALSE)
{
throw;
}
// Get children of class - which are methods.
DWORD numChildren = 0;
if (SymGetTypeInfo(this->m_hProcess, classSymbol->ModBase, classSymbol->TypeIndex, TI_GET_CHILDRENCOUNT, &numChildren) == FALSE)
{
throw;
}
// Get methods info.
if (SymGetTypeInfo(this->m_hProcess, classSymbol->ModBase, classSymbol->TypeIndex, TI_FINDCHILDREN, methods) == FALSE)
{
throw;
}
// Retrieve all methods.
for (DWORD i = 0; i < numChildren; ++i)
{
ULONG curChild = methods->ChildId[i];
// Resolve function.
Function resolvedFunction;
this->m_functionResolver->Resolve(classSymbol->ModBase, curChild, resolvedFunction);
// Add the resolved function to the output.
resolvedMethods.push_back(resolvedFunction);
}
步骤2是繁琐。这只是文本比较和处理。
如何注入魔力ASM改变方法的行为:(完整的源代码可在https://github.com/mazong1123/injectorpp/blob/master/injectorpp/BehaviorChanger.cpp)
// A magic function to change the function behavior at runtime
//
// funcAddress - The address of the function to be changed from.
// expectedReturnValue - The return value should be changed to.
void BehaviorChanger::ChangeFunctionReturnValue(ULONG64 funcAddress, int expectedReturnValue)
{
// The purpose of this method is to change the return value
// to what ever int value we expected.
// Therefore, we just need to inject below asm to the header of specific function:
//
// mov eax, expectedValue
// ret
//
// Above asm code tells the function to return expectedValue immediately.
// Now let's prepare the asm command.
byte asmCommand[6];
// mov
asmCommand[0] = 0xB8;
// The value.
asmCommand[1] = expectedReturnValue & 0xFF;
asmCommand[2] = (expectedReturnValue >> 8) & 0xFF;
asmCommand[3] = (expectedReturnValue >> 16) & 0xFF;
asmCommand[4] = (expectedReturnValue >> 24) & 0xFF;
// ret
asmCommand[5] = 0xC3;
WriteProcessMemory((HANDLE)-1, (void*)funcAddress, asmCommand, 6, 0);
}
看起来很有趣。我很想看到GCC/Clang类似的东西。 – hedayat 2017-09-02 10:28:04
@hedayat我打算这样做。最近我几乎完成了x86 Windows部分。保持调谐。 – 2017-10-01 06:39:03
为了让您的“生产”代码干净,我发现它有用执行此操作: template class TestedClassTemplate {...},然后执行typedef TestedClassTemplate TestedClass; –
smehmood
2011-04-28 19:28:53
请参阅http://stackoverflow.com/q/1127918/49972了解您提出建议后果的信息。 – 2011-05-26 20:19:57
我在'MOCK_METHOD(MethodA,bool())中遇到问题;''https://stackoverflow.com/questions/46542373/how-to-mock-non-virtual-methods-using-googlemock?noredirect=1#comment80038793_46542373我收到上述问题中指定的错误 – CMouse 2017-10-03 10:33:44