tags:

views:

165

answers:

2

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

A: 

Do you really need a pattern for that? Why can't you just unit test the static methods straight away? They seem to be public from your code.

CesarGon
i need to mock, not test. i need to replace the OS-level calls which dummy functions during testing.
moogs
Oh, sorry, I misread your question. My bad. :-)
CesarGon
+5  A: 

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.

Ngu Soon Hui
I find it ironic how static members are "frowned upon", when what we really should frown upon are mocking frameworks that cannot mock static members. After all, if TypeMock can do this, why can't OSS frameworks do the same?
Pavel Minaev
The reason for this is that most mocking frameworks implement mocks using the Proxy pattern, which essentially requires inheritance, and of course static classes cannot be inherited from. Typemock uses an entirely different approach, by redirecting method calls with IL injection, which gives it the power to mock static calls.
womp
Because it is *too expensive to be developed for free* (http://stackoverflow.com/questions/1534119/is-there-any-open-source-mocking-framework-resembling-typemock)
Ngu Soon Hui