Microsoft Fakes Part Two

date_range  26 June 2014

.Net Development
Unit Testing
Tutorial

I think this has probably long gone past the point at which I can call this “Part Two”, but I’m going to anyway.

I’m sure everyone has looked at the official documentation for Fakes by now, but there are a few things you can do with Fakes that isn’t mentioned.

Generic Methods

Ever thought you couldn’t mock a generic methods functionality? That’s because a generic methods stubs can’t be set in the normal way be setting the replacement via an assignment, instead you inject it:

Shim Base Classes

One of the more odd things that Fakes does is ignore inherited methods that aren’t overridden. So how do you hit those mysterious base methods? You shim the base class of course then inject you instance. Quite a common scenario this fits into is testing MVC Controller instances and faking out some of the properties such as Request.

Faking Assemblies that contain obsolete code

Recently I came across a third party assembly that contained a class that inherited an obsolete class. Fakes refused to build the Fakes Assembly because it by default ignores obsolete types. To get around this in the .fakes file you just Remove obsolete classes.

comments powered by Disqus

chevron_left Archive