chevron-thin-right chevron-thin-left brand cancel-circle search youtube-icon google-plus-icon linkedin-icon facebook-icon twitter-icon toolbox download check linkedin phone twitter-old google-plus facebook profile-male chat calendar profile-male

Typemock Isolator .NET Examples

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

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.


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


In order to test how this works, we need to pass a Fake HttpContext here is an example 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 Typemock 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 how.


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


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.


Faking a Singleton – Unique Feature

Faking a singleton can be ticky 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 elegently, 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 anythink as the static constructor is called once, but when the singleton has not been created, we wont 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 statment will fake the Instace 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 Dependancy 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 Dependancy object.


Note: when we fake the constructor – the next instance is a fake and all methods are recursivly 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 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.


Lets test that the session was really abandoned