HippoMocks documentation says that it can mock C function including Windows API function, but I could not find any example for it. Can anyone give an example for windows API function mocking?
-
2That project looks moribund. Are you sure you want to rely on it? – David Heffernan Mar 21 '12 at 18:43
-
Yeah it seems so. Know any alterative with same functionality? – goto Mar 22 '12 at 03:45
1 Answers
I need to get a new release out, that's for sure.
You can mock an API function as you would any other function, except that you don't specify any object to call it on (because it doesn't have any). I've tested it a lot on Linux with the regular libc API functions and that worked incredibly well. Windows should be no different, but this is why my example will be exit:
void test() {
MockRepository mocks;
mocks.ExpectCallFunc(&exit).With(2).Throw(std::exception());
}
Note that this works for any function, including those that are specified never to return. If you do tell HippoMocks to mock a function that shouldn't return, the return code may not have been generated causing errors. Try throwing a test-specific exception instead. Now that I think of it, that was on Windows using VS2008 where it literally had no opcodes beyond the call to exit.
Hope you can get it to work. Be sure to take the most current commit on Git (from Assembla) as the last release doesn't contain this yet.

- 7,184
- 1
- 29
- 50
-
Can you please do a release soon as the updated code from SVN is not compiling on VS2010. Also it is saying that ExpectCall() requires two parameters. I am giving NULL in the first. `mocks.ExpectCall(NULL, CreateFileMapping).Return(INVALID_HANDLE_VALUE);` One of the error is ` error C2784: 'TCall
&MockRepository::RegisterExpect_(Z2 *,Y (__thiscall Z::* )(A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P) const,const char *,const char *,unsigned long)' : could not deduce template argument for 'Z2 *' from 'int' ` – goto Mar 28 '12 at 14:23 -
HippoMocksTest solution compiles and passes all the 48 tests in debug and release mode on VS2008 and VS2010. So there is something wrong in the way I am using the ExpectCall(). If I write the code in your example, the compiler says `not enough actual parameters for macro 'ExpectCall'` – goto Mar 29 '12 at 05:53
-
Aah... let me just check that, I may have had to change it as you can't apparently overload macros. There's ExpectCallDestructor for destructors (since you can't take the address of a destructor, but you can mock it), ExpectCall for regular virtual member functions and ExpectCallFunc for flat functions. – dascandy Mar 29 '12 at 09:37
-
Yep, my bad. The test itself is here: http://www.assembla.com/code/hippomocks/git/nodes/master/HippoMocksTest/test_cfuncs.cpp . It has a local function that it replaces with another return value. – dascandy Mar 29 '12 at 09:38
-
I am working on creating a class with static methods that are wrappers around windows api functions. Can you give me an example of that? – goto Apr 02 '12 at 08:26
-
Static class members are just flat functions, same example except "Class::funcname" instead of "exit". – dascandy Apr 03 '12 at 10:32
-
@dascandy: do you know how it works ? How we can intercept a free function call (like the exit function) ? – Guillaume Paris Jul 31 '14 at 13:10
-
@Guillaume07 Using HippoMocks, use "ExpectCallFunc(exit).With(0).Throw(...)". You can look at the code, specifically the "Replace" struct at about line 800, that does the actual replacing and memory protection handling. It works for x86, x86-64 and ARM platforms. – dascandy Aug 01 '14 at 09:51
-
@dascandy:I well know the syntax to do that, but I'm wondering how does it work? how in c++ we can make a free function call to be intercepted – Guillaume Paris Aug 01 '14 at 09:52
-
@Guillaume07 Not actually in legal c++; you can either make it a function pointer or "hack" the byte code in the function. You know, maybe this is a question on itself that you can ask separately? Other people could post responses too and so on. – dascandy Aug 01 '14 at 09:53