Microsoft Fakes And Shims: Unit Testing Without Database Dependency

  • Post published:June 22, 2015

An application architecture was developed by vteam #366 to test all its functionality and APIs, a separate Test Project was serving this purpose. As per Client’s requirement, we had to develop a test driven approach that could work without actual data store/database dependency and generate dummy values to be used instead of actual data from the database.

Problem:

The problem faced by vteam #366 was that Client refused to share the actual data from database to test code. Reason being, he did not want his actual and sensitive data to be shared and used for testing purpose in few scenarios and modules.

Solution:

There are different Open Source mocking frameworks available that can be used to complete this requirement in .NET like NMock, Rhino Mocks, Moq etc., but Microsoft Fakes is a mocking framework provided by Microsoft itself. That is why, it was used by vteam #366 for this scenario where test stubs and shims were created to replace the actual code and parts of the application dealing with database calls. This framework is mainly used to isolate the code under test. Following were the basic steps followed and an example where shims types were used to replace the actual database calls:

  • Add Fakes Assembly
  • Use ShimsContext
  • Write a test with Shims

microsoft-fakes-1microsoft-fakes-2Conclusion:

vteam #366 identified during the deployment process that one need to have Visual Studio Premium or Ultimate Edition installed on the specific machine to use this framework, which is the major drawback as it is pretty costly. But on the other hand,  this framework also have some advantages as well which are as follows:

  • No more need to find and install open source mocking frameworks
  • Easy implementation and replacement of calls to any assembly
  • Extensive support available as compared to open source projects