Tuesday, October 14, 2008

An introduction to Mocking

Mocking

When you want to test certain function/feature which is dependent on some other features which might take some time which might not be available at that time then you can Mock that particular module paticular procedure and you will get the result that you want to continue writing your Unit-Test. For example you want to mock the internet connection you can do that. So you want to let say that some of the features are only available when internet connection is established. Now you are in a situation where you can't continue writting your unit-test as there is no internet connection available. So you can mock the internect connection and write your unit-test with Mock object. The Mock object ensures that there is an internet connection but in reality there is no internet connection. So you can basically control there resturn type, you can control outside behaviour of that real object but it is not in reality the real object.

Mocking is an integral part of unit testing. Although you can run your unit tests without use of mocking but it will drastically slow down the executing time of unit tests and also will be dependent on external resources.

There are different types of Mocking frameworks available which allow us to create Mock Objects.
1. Rhino Mock
2. NMock2
3. TypeMock.NET
4. EasyMock.NET
5. Moq

TypeMock in not free.

Links
1. http://www.gridviewguy.com/Articles/447_Introduction_to_Mocking.aspx