Mocking a static class

I have a static class that wraps some native methods from winspool:

public static class WinSpool { [DllImport("winspool.drv")] public static extern int OpenPrinter(string pPrinterName, out IntPtr phPrinter, IntPtr pDefault); ... //some more methods here } 

I would like to make fun of them for unit testing, but could not find a template for this. (Does everyone avoid static classes?)

+7
mocking static-classes
source share
2 answers

Yes, the static class usually frowned in the area of ​​unit testing and ridicule . AFAIK does not contain open source frames (e.g. Rhino Mocks ) supports static class, mocking

If you absolutely and positively have to mock a static class, I am afraid that you should go to Typemock , which is not free.

+10
source share

Microsoft has released the Fakes framework for Visual Studio 2012 Ultimate, which allows static types to mock using pads. Gaskets are fake assemblies that provide delegates with all the methods available for access. Calls are then routed through an event filter, and wherever a test has configured one of its delegates, the delegate processes the call. Not that anyone wants to promote static types that are not handled by dependency injection. The preferred direction is the transition from the need to use gaskets to the use of plugs using DI.

+2
source share

All Articles