Question

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?)

Was it helpful?

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.

OTHER TIPS

Microsoft has produced a Fakes framework for Visual Studio 2012 Ultimate that allows static types to be mocked using shims. Shims are "fake" assemblies that provide delegates for all externally accessible methods. Calls are then routed through the event filter, and wherever the test has set up one of its delegate, the delegate handles the call. Not that anyone wants to encourage static types that aren't handled through dependency injection. The prefered direction is to move away for the need for shims and toward the use of stubs using DI.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top