Sunday, April 26, 2015

Microsoft Fakes

A mocking framework enables you to provide a fake implementation of a type or object, along with logic that verifies how calls were made to the mocked object. There are several good mocking frameworks currently available in the community, including Moq, Rhino, and NMock. Although these tools have strong followings and a good reputation, there was still a need to provide a mocking framework to customers who may be unable to utilize open source or third-party tools. Hence, the Microsoft Fakes framework in Visual Studio 2013.

The Fakes framework is derived from the Moles project by Microsoft Research. The Fakes framework is not backward-compatible with Moles, but the migration is straightforward.

Stubs are concrete implementations of interfaces and abstract classes that can be passed into the system being tested. A developer provides method implementations via .NET delegates or lambdas. A stub is realized by a distinct type that is generated by the Fakes framework. As such, all stubs are strongly typed. You cannot use stubs for static or non-overridable methods. Instead, you should use shims in those instances.

Shims are runtime method interceptors. They enable you to provide your own implementation for almost any method available to your code in .NET, including types and methods from the .NET base class libraries.

Task Stub/Shim Reason
Performance Stub The runtime code-rewriting used by shims introduces some performance issues at runtime. Stubs do not do this.
Static Methods Shim Stub can only influence overridable methods. They cannot be used for static, non-virtual, and sealed virtual methods.
Internal Types Stub/Shim Both stubs and shims can be used with internal types made accessible through the InternalsVisibleToAttribute attribute.
Private Methods Shim Shim types can replace private methods if all the types on the method signature are visible.
Interfaces/Abstract Methods Stub Stubs implement interfaces and abstract methods that can be used for testing. Shims can't do this because they don't have method bodies.
Technorati Tags: ,,

No comments:

Post a Comment