Isolator For .NET Examples- Unit Test Project

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

Examples:

  • Faking Static Methods
  • Faking Sealed Types
  • Recursive Faking
  • Setting Behavior on Chain
  • Faking a Singleton
  • Faking Live Objects
  • Faking Constructors
  • Faking Static Constructors
  • Verifying Calls

 

Faking Static Methods

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

The following code acts differently on a leap year

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


Faking Sealed Types

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

The following code requires an HtppContext in order to work


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


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 Isolator requires only one line. Here is an example.

The following code requires an HtppContext in order to work


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:


Note: this feature makes our tests robust and eliminates maintenance when production code changes call to the 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


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


Faking a Singleton – Unique Feature

Faking a singleton can be tricky as it is set only once in the code, so faking it can affect other tests. Here is the correct way to fake a singleton.

To solve this elegantly, we use 2 statements the first will fake the static constructor, that is the place where the singleton creates the instance. When the singleton has already been created in a previous test, this won’t do anything as the static constructor is called once, but when the singleton has not been created, we won’t create the instance. Typemock will make sure that the static constructor will be run the next time an unfaked singleton is used (Cool 🙂
The other statements will fake the Instance property to return a fake one.


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


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


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

In order to test this, we need to fake the Dependency object.


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


Faking Static Constructors – Unique Feature

This example will show how to faking static constructors.


Note: after we fake a static constructor – the type will be in an invalid state, Typemock will ensure 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.


Lets test that the session was really abandoned