Mocking a Static Class

.net, mocking, static-classes

Solution

Yes, static class is generally frowned upon in the field of unit testing and mocking. AFAIK no open source mocking framework ( such as Rhino Mocks) support static class mocking

If you absolutely and positively must mock static class, then I afraid that you must go for Typemock, which is not free.

Problem

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 mock them for unit testing, but couldn't find a pattern for this. (Does everyone avoid static classes?)

Original source

Related problems