TypeMock Isolator is a mocking framework for .Net. It uses the .Net profiling API to do its work. This makes it possible for TypeMock to mock parts of your code that other frameworks cannot. TypeMock makes it easy to write unit tests. TypeMock provides three ways to fake dependencies:

  • Relective Mocks. Reflective mocks uses reflection and a string based API,
  • Natural Mocks. It is a Record-Replay but strongly typed API,
  • Arrange-Act-Assert (AAA) API.

In this post I want to show you how to mock the AppSettings property of the ConfigurationManager class using the Reflective and Natural Mocks patterns.

the Reflective Mocks way

reflectivemocks

Notice the ClearMocks attribute (line 16) on the testclass. It tells TypeMock to cleanup its mock administration before a test is run. Also notice the VerifyMocks attribute (line 20) on the testmethod. This attribute cause TypeMock to verify the mocks in the test. These attributes are used by Natural Mocks also.

MockManager.Mock (line 29) is used to get fake of the ConfigurationManager. With the fake you can tell TypeMock that the AppSettings property returns a NameValueCollection. You can use either ExpectGet or ExpectGetAlways. I used the latter because TypeMock will always return the value you setup. ExpectGet will only return the value once. Every next call to AppSettings will throw an exception.

As you can see in the example above I created a NameValueCollection and added the key-value pair “ApplicationName”- “TypeMockDemo”. With ExpectGetAlways (line 30) I make sure the my collection is always returned. Just the way I like it.

This is all you have to do to setup ConfigurationManager.AppSettings. Now you can call the code you want to test like you always did. Look at the picture below for the implementation of the ApplicationContext class.

appContext

The Natural Mocks way

Next the same example as above but now implemented with Natural Mocks. The picture below shows the code:

naturalmocks

The setup of the fake is done in the using block. A recorder is created by RecorderManager.StartRecording. The recorder let you setup expectations. In this case I told TypeMock that I expect a call to the  ConfigurationManager.AppSettings.Get method. See line 45. After setting up the expectation I told the recorder to return “TypeMockDemo” for this expectation. After the setup you can call the code you want to test like you always did.

I do not like line 45 because it is not natural. I want to setup the expectation like this: ConfigurationManager.AppSettings[“ApplicationName”] = “TypeMockDemo“;. I could not get that to work. Maybe someone else knows how to do that. Someone?

Finishing up

I am going to show more examples of how to mock using TypeMock. Also I am going to rewrite the examples with RhinoMocks and Moq mocking frameworks.

Updated: The pictures were wrong. I copy-pasted the pictures. If you do that Live Writer gives them the same name. My blog shows the same image for all images. I corrected that.