Writing and running your first unit test in C#
Step 1 – Create a new Test Project
This step shows you how to create a new test project in Visual Studio, and add the Typemock references you need.
- Open Visual Studio, click ‘File’ and create a new Test Project.
- Right click ‘Project Properties’, click ‘Add References’ and add ‘Typemock Isolator core DLL’ and ‘Typemock Isolator C# API’For this example test, you’ll need a reference to System.Windows.Forms as well.
Step 2 – Write unit tests
This section will walk you through the three stages in unit tests that use the C# API to isolate.
-
-
- Arrange – Set up your faked object’s behavior
- Act – Call your test code
- Assert – Make sure that the tests were called
-
-
- In the new test project we opened in Visual Studio, go to UnitTest1.cs in the ‘Solutions’ column and add the following test code to the beginning of the file:
using System.Windows.Forms;
using TypeMock.ArrangeActAssert;
using TypeMock.ArrangeActAssert;
Add the Isolated attribute to the test class:
[TestClass, Isolated]
public class Test0_Tutorial
public class Test0_Tutorial
Example Test 1 – Simple test using MessageBox
This example shows how to ignore a method call, and verify it was called during our test. We’ll use the following APIs in this test:
- Isolate.WhenCalled – The method inside the WhenCalled clause will be ignored, and will return a fake value
- Isolate.Verify.WasCalledWithExactArguments – Verifies that the method inside the WhenCalled clause was called during the test with the exact arguments
The following takes place in the code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
[TestMethod] public void SimpleTestUsingMessageBox() { // Arrange 1 Isolate.WhenCalled(()=> MessageBox.Show(String .Empty)).WillReturn(DialogResult.OK); // Act 2 MessageBox.Show ("This is a message"); // Assert 3 Isolate .Verify.WasCalledWithExactArguments(()=>MessageBox.Show("This is a message")); } |
- Set all calls to MessageBox.Show to be ignored, and return a fake value.
- Call MessageBox.Show – notice that when you run the test, the MessageBox doesn’t actually appear.
- Check that the static function ‘Show’ was called during the test with the parameter “This is a message”.
Example Test 2 – Complex Test
This example shows how to test the interaction between two classes:
- SomeClass – Has a shared sub called MyMethod
- UserOfSomeClass – Has a sub called DoSomthing
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
public class SomeClass { public static void MyMethod() { // do work } } public class UserOfSomeClass { public void DoSomeThing() { try { SomeClass.MyMethod(); } catch(Exception exc) { MessageBox.Show("Exception caught: " & exc.Message); } } } |
This test checks that the user sees every exception thrown from SomeClass using a MessageBox.
-
- The following APIs are used in the test:
-
- We’ll use the following APIs in this test:
- Isolate.WhenCalled(..).WillThrow – The method inside the WhenCalled clause will throw a specified exception when called
- Isolate.WhenCalled(..).WillReturn – The method inside the WhenCalled will be ignored, and return a fake value
- Isolate.Verify.WasCalledWithExactArguments – Verifies that the method inside the WhenCalled clause was called with the exact arguments during the text execution
[TestMethod]
public void ComplexTest()
{
// Arrange
1aIsolate.WhenCalled(()=> SomeClass.MyMethod()).WillThrow( new Exception (“foo”)); 2aIsolateWhenCalled(()=> MessageBox.Show(String.Empty)).WillReturn(DialogResult.Cancel); // Act 2UserOfSomeClass user = new UserOfSomeClass();user.DoSomthing(); 3Isolate .Verify.WasCalledWithExactArguments(()=> MessageBox .Show(“Exception caught: foo”)); }
public void ComplexTest()
{
// Arrange
1aIsolate.WhenCalled(()=> SomeClass.MyMethod()).WillThrow( new Exception (“foo”)); 2aIsolateWhenCalled(()=> MessageBox.Show(String.Empty)).WillReturn(DialogResult.Cancel); // Act 2UserOfSomeClass user = new UserOfSomeClass();user.DoSomthing(); 3Isolate .Verify.WasCalledWithExactArguments(()=> MessageBox .Show(“Exception caught: foo”)); }
-
- The following takes place in the code:
-
- The actual test code – Create a new instance of UserOfSomeClass and call the DoSomething method.
-
- Check that MessageBox.Show was called during our test with ‘Exception caught: foo’.
- Here is the full source code we used in our examples:
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Windows.Forms;
using TypeMock.ArrangeActAssert;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Windows.Forms;
using TypeMock.ArrangeActAssert;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
[TestClass, Isolated] public class Test0_Tutorial { [TestMethod] public void SimpleTestUsingMessageBox() { // Arrange Isolate.WhenCalled(()=> MessageBox.Show(String.Empty)).WillReturn(DialogResult.OK); // Act MessageBox.Show("This is a message"); // Assert Isolate.Verify.WasCalledWithExactArguments(()=> MessageBox.Show("This is a message")); } [TestMethod] public void ComplexTest() { // Arrange Isolate.WhenCalled(()=> SomeClass.MyMethod()).WillThrow( new Exception ("foo")); Isolate.WhenCalled(()=> MessageBox.Show(String.Empty)).WillReturn( DialogResult.Cancel); // Act UserOfSomeClass user = new UserOfSomeClass (); user.DoSomthing(); // Assert Isolate.Verify.WasCalledWithExactArguments(()=> MessageBox.Show("Exception caught: foo")); } } public class SomeClass { public static void MyMethod() { // do work } } public class UserOfSomeClass { public void DoSomething() { try { SomeClass.MyMethod(); } catch ( Exception exc) { MessageBox .Show("Exception caught: " & exc.Message); } } } |
-
- For more help on using Typemock Isolator, read the following:
-
You’re also invited to read our blog
-
-
- to see more advanced usages of Isolator and unit testing information, and if you have any questions, feel free to use our
-
- .