Interested in a
Personal Demo ?


Name* :
Please Enter your Name
Company E–Mail* :
Please Enter a Valid Email

Testimonials


"Holy crap, this thing is hot. In technical terms, you might call it the *bomb-diggity*. I haven’t been this stoked about a technology for quite some time..."
Travis Illig, Senior Software Engineer, Fiserv
Success stories

PurchaseOptions


US Toll Free
866–895–4680
Outside US
+44–203–150–0293
Get your printable quote
Buy online

 

Writing and running your first unit test in C# 

 You can find the code used in this tutorial in Test0_Tutorial.cs, in the Isolator installation folder, as part of the Typemock.Example.CSharp solution.

 

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.

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.

     1. Arrange – Set up your faked object’s behaviour

     2. Act - Call your test code

     3. 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;

 

Add the Isolated attribute to the test class:

 

[TestClass, Isolated]
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:

The following takes place in the code:

 

[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"));
}

 

 

  Example Test 2 - Complex Test

This example shows how to test the interaction between two classes:

 

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:

 

[TestMethod]
public void ComplexTest()
{
 / Arrange
1a Isolate.WhenCalled(()=> SomeClass.MyMethod()).WillThrow( new Exception("foo"));
2a Isolate.WhenCalled(()=>MessageBox.Show(String.Empty)).WillReturn(DialogResult.Cancel);

 / Act
2 UserOfSomeClass user = new UserOfSomeClass();
user.DoSomthing(); 

 / Assert
3 Isolate .Verify.WasCalledWithExactArguments(()=> MessageBox .Show("Exception caught: foo"));
}

 

The following takes place in the code:

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;


[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.DoSomething(); 

  / 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);
  }
 }
}

 

 

How to Start Unit Testing .NET Projects  Download Free Trial    Basic unit testing: Mocking and
Faking
     

Free Webinar: Triumph Over Legacy Code Writing Your First Unit Test in C# Difference Between Integration Tests & Unit Tests


     
                                                                   

 

                                                      

                                                                      

                          

 

 

Read our blog to see more advanced usages of Isolator and unit testing information: