Sample 1: Ignoring a Method Call
This sample shows:
• How to ignore a method call
• How to verify that the method was actually called during the test
APIs Used
Method |
Description |
Isolate.WhenCalled() |
The method inside the Isolate.WhenCalled() will be ignored and will return a fake value. |
Isolate.Verify.WasCalledWithExactArguments() |
Verify that the method inside the Isolate.WhenCalled() was called during the test with the exact arguments. |
Scenario
1. Set all calls to MessageBox.Show() to be ignored and return a fake value.
2. Call MessageBox.Show(). When running the test the MessageBox does not appear.
3. Check that the static function Show() was called during the test with the parameter "This is a message".
Code
C# [TestMethod, Isolated] public void SimpleTestUsingMessageBox() { // Arrange Isolate.WhenCalled(() => MessageBox.Show(null)).WillReturn(DialogResult.OK); // Act MessageBox.Show("This is a message"); // Assert Isolate.Verify.WasCalledWithExactArguments(()=>MessageBox.Show("This is a message")); }
VB
<TestMethod, Isolated>
Public Sub SimpleTestUsingMessageBox()
' Arrange
Isolate.WhenCalled(Function() MessageBox.Show(Nothing)).WillReturn(DialogResult.OK)
' Act
MessageBox.Show("This is a message")
' Assert
Isolate.Verify.WasCalledWithExactArguments(Function() MessageBox.Show("This is a message"))
End Sub