Controlling PInvoke Methods
When to Use
When your test requires a pinvoke method to return a specific value.
Syntax
C# Isolate.WhenCalled(() => <pInvoke-method()>).<behavior>;
VB
Isolate.WhenCalled(Function() <pInvoke-method()>).<behavior>
Samples
C# [DllImport("kernel32.dll", SetLastError = true)] [return: MarshalAs(UnmanagedType.Bool)] static extern bool Beep(uint dwFreq, uint dwDuration); [TestMethod] public void PInvokeMethod_Example() { Isolate.WhenCalled(()=> Beep(0,0)).WillReturn(false); var result = Beep(100, 200); Assert.AreEqual(false, result); }
VB
<DllImport("kernel32.dll", SetLastError:=True)>
Private Shared Function Beep(dwFreq As UInteger, dwDuration As UInteger) As <MarshalAs(UnmanagedType.Bool)> Boolean
End Function
<TestMethod(), Isolated()>
Public Sub PInvokeMethod_Example()
Isolate.WhenCalled(Function() Beep(0, 0)).WillReturn(False)
Dim result = Beep(100, 200)
Assert.AreEqual(False, result)
End Sub