Blogger Widgets
  • Sharing Photos using SignalR
  • TFS Extenstion - allows copy work items between projects
  • Displaying jquery progressbar with ajax call on a modal dialog
  • Managing windows services of a server via a website
  • Exploring technologies available to date. TechCipher is one place that any professional would like to visit, either to get an overview or to have better understanding.

Search This Blog

Monday, 7 October 2013

Mock frameworks for .NET Part 1

Mock frameworks are used for unit testing your code and are used for Test Driven Development (TDD). A mock framework should support Arrange Act Assert (AAA) pattern. There are various mock frameworks available and choosing the right one is not easy. Before looking at various frameworks available let's understand some basics of what a framework should have to be known as a Mock framework.

Mock Interfaces & abstract classes

Interfaces or Abstract classes allows Mock framework to be able to create stubs, A stub is a small block of code that is replacement of another component or code block. TDD approach of R-G-R (Red-Green-Refactor) is an example where a stub might be useful. In order to make a test successfull (Green), you would need a stub for places where actual definition is either not written or unknown.

Mock Everything in .NET

Able to mock everything in a class, such as
  • mock dependent objects
  • non-virtual methods
  • sealed classes
  • static methods
  • static classes
  • non-public members
  • non-public types

Mock Legacy Code

Assume some legacy code that has got some core business logic but has been designed/coded way before TDD has even started
  • no interfaces
  • no abstract classes
  • has dependencies

Auto Recursive Fakes

Once a mock is created some methods and properties will automatically return non null values such as interfaces, delegate, virtual class etc. This is also known as recursive mock.

Fake Dependencies

If a mock is dependent on another object, framework should allow to either populate or assign dependency so as to make the test succeed. Most of the frameworks will support it by default.

Unit Test Integration

Allow easy integration with unit test tools such as NUnit, MSTest, Xunit etc.

Build Server Integration

Mock framework should allow you to be able to automate unit tests as part of your build & continuous integration. It should be able to support various build servers such as TFS, MSBuild, Nant, CruiseControl.NET etc

Code coverage Integration

Mock framework should allow to be integrated with 3rd party code coverage so as to produce various metrics that can easily point out areas of code that has not been covered by a unit test or has been wrongly written. It should be able to support various 3rd party code coverage software such as NCover, MSTest Code Coverage, ReSharper etc.

All of the these are only related to .net code. The mock framework features that are required for javascript mocking is out of scope of this article. The list of mock frameworks can be seen at Mock frameworks for .NET Part 2

Intelligence is the ability to adapt to change.
Stephen Hawking


Post a Comment

Copyright © 2013 Template Doctor . Designed by Malith Madushanka - Cool Blogger Tutorials | Code by CBT | Images by by HQ Wallpapers