Mocking P/Invoke calls?

4 posts, 0 answers
  1. Robert
    Robert avatar
    6 posts
    Member since:
    Aug 2012

    Posted 11 Apr 2011 Link to this post

    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
  2. Ricky
    Admin
    Ricky avatar
    467 posts

    Posted 13 Apr 2011 Link to this post

    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
  3. DevCraft R3 2016 release webinar banner
  4. Robert
    Robert avatar
    6 posts
    Member since:
    Aug 2012

    Posted 14 Apr 2011 Link to this post

    Ok, thanks. Are there any plans to support this in the future, or is it too exotic?

    Regards, Robert
  5. Ricky
    Admin
    Ricky avatar
    467 posts

    Posted 19 Apr 2011 Link to this post

    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
Back to Top