This is a migrated thread and some comments may be shown as answers.

Mocking P/Invoke calls?

3 Answers 123 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Robert
Top achievements
Rank 1
Robert asked on 11 Apr 2011, 01:47 PM
Hi,

is it possible to mock P/Invoke (Windows API) calls using JustMock commercial edition? I have a helper class which uses a few APIs from advapi32.dll and kernel32.dll to log-on to a remote file share and would like to unit test it and assert whether the right APIs were called with the right parameters.

E.g. the API
[DllImport("advapi32.dll", SetLastError = true)]
>
private static extern bool LogonUser(string lpszUsername, string lpszDomain, string lpszPassword, int dwLogonType, int dwLogonProvider, ref IntPtr phToken);


is called in my code and I'd like to assert that the correct user name, domain, password etc. were passed to it.

Regards, Robert

3 Answers, 1 is accepted

Sort by
0
Ricky
Telerik team
answered on 13 Apr 2011, 11:55 AM
Hi Robert,

Mocking an extern method directly is not supported.

As a workaround, if you're calling the extern from your own code, you can wrap the call by:

Code:
public static bool InternalLogonUser(string lpszUsername, string lpszDomain, string lpszPassword, int dwLogonType, int dwLogonProvider, ref IntPtr phToken)
{
    return LogonUser(lpszUsername, lpszDomain, lpszPassword, dwLogonType, dwLogonProvider, ref phToken);
}

And mock the behavior on the wrapper call.

Should you have anymore questions please don't hesitate to write us back.

Kind Regards,
Ricky
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Robert
Top achievements
Rank 1
answered on 14 Apr 2011, 01:33 PM
Ok, thanks. Are there any plans to support this in the future, or is it too exotic?

Regards, Robert
0
Ricky
Telerik team
answered on 19 Apr 2011, 07:47 AM
Hi Robert,

Thanks again for your reply. I have already included that into our product backlog and we would love to have that support in JustMock. However, due to some technical issues with P/Invoke and profiling, it is not possible to intercept such calls at the moment.

Kind regards,
Ricky
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Tags
General Discussions
Asked by
Robert
Top achievements
Rank 1
Answers by
Ricky
Telerik team
Robert
Top achievements
Rank 1
Share this question
or