Typemock Isolator .NET Examples

Following are some examples of using the Typemock Isolator for .NET 

You will need to setup a unit test project first. You will also find runnable examples under the installation directory of Typemock.

Examples:


 

Faking Static Methods

Faking static method is easy with Typemock Isolator, here is an example.

The following code acts differently on a leap year

public static int DoSomethingSpecialOnALeapYear()
{
   if ((DateTime.Now.Month == 2) && (DateTime.Now.Day == 29))
      return 100;
   else
      return 0;
}

In order to test how this works on a leap year, we need to mock the static method DateTime.Now() here is an example how.

[TestMethod,Isolated] 
public void FakingDateTime()
{
	// Arrange - Fake DateTime to think that it is 29th of Feb 2016
	Isolate.WhenCalled(() => DateTime.Now).WillReturn(new DateTime(2016, 2, 29));

	// Act 
	int result = MyCode.DoSomethingSpecialOnALeapYear();

	// Assert 
	Assert.AreEqual(100, result);
}

 


Faking Sealed Types

Faking sealed types is easy with Typemock Isolator, here is an example.

The following code requires an HtppContext in order to work

public static bool SignOut(HttpContext current)
{
   if (signedIn)
   {
	   HttpSessionState session = current.Session;
	   session.Abandon();
	   return true;
   }
   return false;
}

In order to test how this works, we need to pass a Fake HttpContext here is an example how.

[TestMethod,Isolated] 
public void FakingHttpContext()
{
	// Arrange - Fake an HttpContext
	var fakeHttp = Isolate.Fake.Instance();
 	MyCode.signedIn = true;

  	// Act
 	var result = MyCode.SignOut(fakeHttp);

  	// Assert
 	Assert.AreEqual(true, result);
}

 Note this unique feature: a fake instance will automatically fake all methods and chain of methods, called from that instance.

 


Recursive Faking - Unique Feature

Recursive Faking is the ability to fake Fake everything you don't care about.
All methods of the fake will return fakes, and they in turn will return fakes, and so on.

Doing this with Typemock Isolator requires only one line. Here is an example.

The following code requires an HtppContext in order to work

public static bool SignOut(HttpContext current)
{
   if (signedIn)
   {
	   HttpSessionState session = current.Session;
	   session.Abandon();
	   return true;
   }
   return false;
}

In order to test how this works, we need to pass a Fake HttpContext and to make sure that Session returns a fake HttpSessionState. here is an example how.

[TestMethod,Isolated] 
public void FakingHttpContext()
{
	// Arrange - Fake an HttpContext
	var fakeHttp = Isolate.Fake.Instance();
  	MyCode.signedIn = true;    	
        // Act  
	var result = MyCode.SignOut(fakeHttp);
    	// Assert
  	Assert.AreEqual(true, result);  
}

 Note: this feature makes our tests robust and eliminates maintainance when production code changes calls to faked object.

 


Setting behavior on a chain - Unique Feature

Setting behavior on a chain of calls can be done in one call. Here is an example.

The following code requires a Process in order to work

public static bool IsMySiteNameTypemock(Process process)
{
	var name = process.MachineName;

	if (process.MainModule.Site.Name.StartsWith("Typemock"))
		return true;
	else
		return false;
}

In order to test how this works, we need to pass a Fake Process and to simulate it 's Name. Here is an example how.

[[TestMethod,Isolated] 
public void SetBehaviorOnChainExample()
{
	// Arrange - Fake a Process
	var fake = Isolate.Fake.Instance();

 	// Set Fake Process Name
 	Isolate.WhenCalled(() => fake.MainModule.Site.Name).WillReturn("Typemock rocks");

  	// Act
 	var result = MyCode.IsMySiteNameTypemock(fake);

  	// Assert
 	Assert.AreEqual(true, result); 
}

 


 

 

Faking Live Objects - Unique Feature

A live object is a test object which has been instantiated normally. It is possible to modify a live object's behavior . Here is an example.

The following code requires that CheckSecurity will pass in order to work

public class Dependency
{
	public virtual void CheckSecurity(string name, string password)
	{
		throw new SecurityException();
	}
}

public class ClassUnderTest
{
	public int Calculate(int a, int b, Dependency dependency)
	{
		dependency.CheckSecurity("typemock", "rules");

		return a + b;
	}
}

In order to test this we need to fake CheckSecurity. In this example we will create a real Dependancy instance and fake ONLY the CheckSecurity method.

[TestMethod,Isolated]
public void FakeMethod_OnLiveObject()
{
	var dependency = new Dependency();

	// Fake the CheckSecurity method only
	Isolate.WhenCalled(() => dependency.CheckSecurity(null, null)).IgnoreCall();

	var classUnderTest =  new ClassUnderTest();

	var result = classUnderTest.Calculate(1, 2, dependency);
	Assert.AreEqual(3, result);
}

 


 

Faking Constructors - Unique Feature

Faking constructors can be done by faking the next instantiation of a type. Here is an example.

The following code requires that CheckSecurity will pass in order to work

public static int Calculate(int a, int b)
{
   var dependency = new Dependency();
   dependency.CheckSecurity("typemock", "rules");
   return a + b;
}

In order to test this we need to fake the Dependancy object.

[TestMethod,Isolated]
public void FakeConstructor()
{
	// Fake the Dependency constructor
	var fakeHandle = Isolate.Fake.NextInstance();
  	var result = ClassUnderTest.Calculate(1, 2);
 	Assert.AreEqual(3, result);
}

 Note: when we fake the constructor - the next instance is a fake and and all methods are recursivly fake.

 


 

Faking Static Constructors - Unique Feature

This example will show how to faking static constructors.

[TestMethod,Isolated]
public void FakeStaticConstructor()
{
    // Fake the Dependency static constructor
    Isolate.Fake.StaticConstructor();
    var result = ClassUnderTest.Calculate(1, 2);
     Assert.AreEqual(3, result); 
} 
// after the this test, the static constructor will be called as normal

 Note: after we fake a static constructor - the type will be in an invalid state, Typemock will esure that the static constructor will be called when needed.

 

 


 

Verifying Calls - Unique Feature

This example will show how to verify calls to a method, when a method doesn't return an object to assert on.

 

public static bool SignOut(HttpContext current)
{
   if (signedIn)
   {
	   HttpSessionState session = current.Session;
	   session.Abandon();
	   return true;
   }
   return false;
}

Lets test that the session was really abandoned

[TestMethod,Isolated] 
public void FakingHttpContext()
{
	// Arrange - Fake an HttpContext
	var fakeHttp = Isolate.Fake.Instance();
	MyCode.signedIn = true;
	
	// Act 
	var result = MyCode.SignOut(fakeHttp);

	// Assert 
	Isolate.Verify.WasCalledWithAnyArgument(()=>fakeHttp.Session.Abandon());
}

 

 

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