Deep Dive With Microsoft Fakes – Shims
Shims are a part of the Microsoft Fakes framework that allow Method interception as a part of testing – including framework methods. This allows for lesser intrusive unit testing, shows Rich Czyzewski in his article, “Noninvasive Unit Testing in ASP.NET MVC4 – A Microsoft Fakes Deep Dive”.
What exactly is non-invasive testing? Rich provides a simple example where a call to FormsAuthentication.SignOut() currently needs to be wrapped in a separate class so that the calling object can be unit tested, since FormsAuthentication depends on HttpContext which is not initialized when running the tests. This is intrusive, because it mandates a change in design just for the sake of testability.
Shims on the other hand can allow call to FormsAuthentication.SignOut() to be intercepted and instead call our own implementation – this allows us avoid having wrapping classes just for the sake of testability and makes testing lesser intrusive.
- Static methods
- Instance methods (for all instances or for a particular instance)
- Normal and Static Constructors
Shims can also be bound to interfaces instead faking classes.
The Fakes Framework can be used with any Unit Testing framework, including NUnit. Rich also has a good comparison between the Moles technology introduced with VS 2010 and Fakes introduced in VS 11 which have very similar features.
Mandating Design changes
>>This is intrusive, because it mandates a change in design just for the sake of testability.
This would clearly mandate a design change...
but in the framework: Make FormsAuthentication independent from HttpContext.
Or abstract the static class FormsAuthentication away...
Sarah Howe Jul 06, 2015