locked
how to mock DllImport ,using shim or stub? RRS feed

  • Question

  • I have several external functions that are reached using DllImport. In my unit testing I want to mock these functions - is there a way to do this?

    My c# class looks like this:

    class MyClass
        {
        [DllImport("MyDll.dll", CharSet = CharSet.Unicode)]
        public static extern int MyFunction(string parameter);
        }
    


    Thanks,


    Monday, August 5, 2013 3:48 AM

Answers

  • Shimming of extern methods is not supported directly. However, if you wrap an extern method using regular static methods, as Amanda said, you can shim that method instead. Here is what the product code would look like.

    public class MyClass
    {    
        public static int MyFunction(string parameter)
        {
            return NativeMethods.MyFunction(parameter);
        }
    
        private class NativeMethods
        {
            DllImport("MyDll.dll", CharSet = CharSet.Unicode)]        
            public static extern int MyFunction(string parameter);
        }
    }

    And here is the test code

    using (ShimsContext.Create())
    {
       ShimMyClass.MyFunction = (parameter) => 42;
       Assert.AreEqual(42, MyClass.MyFunction(string.Empty);
    }

    • Proposed as answer by Amanda Zhu Thursday, August 8, 2013 1:18 AM
    • Marked as answer by carrietks Thursday, August 8, 2013 8:07 AM
    Wednesday, August 7, 2013 5:58 PM

All replies

  • Hello,

    If you want to fakes the function in MyClass, I think that you should use Shim since stub type can’t be used for static methods. For more information, please see ‘Choosing between stub and shim types’ section in this article: Isolating Code Under Test with Microsoft Fakes

    As for how to shim a static method, please see ‘Static methods’ section under ‘Shims for different kinds of methods’ in this article: Using shims to isolate your application from other assemblies for unit testing

    There are some code examples you can reference.

    Best regards,


    Amanda Zhu
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.


    • Edited by Amanda Zhu Thursday, August 8, 2013 1:18 AM edit
    Tuesday, August 6, 2013 7:46 AM
  • Shimming of extern methods is not supported directly. However, if you wrap an extern method using regular static methods, as Amanda said, you can shim that method instead. Here is what the product code would look like.

    public class MyClass
    {    
        public static int MyFunction(string parameter)
        {
            return NativeMethods.MyFunction(parameter);
        }
    
        private class NativeMethods
        {
            DllImport("MyDll.dll", CharSet = CharSet.Unicode)]        
            public static extern int MyFunction(string parameter);
        }
    }

    And here is the test code

    using (ShimsContext.Create())
    {
       ShimMyClass.MyFunction = (parameter) => 42;
       Assert.AreEqual(42, MyClass.MyFunction(string.Empty);
    }

    • Proposed as answer by Amanda Zhu Thursday, August 8, 2013 1:18 AM
    • Marked as answer by carrietks Thursday, August 8, 2013 8:07 AM
    Wednesday, August 7, 2013 5:58 PM