I need to write a unit test for a method that will print a document. Is there an easy way to send the output to the Windows equivalent of /dev/null? I’m guessing that having our build server print a document on every check in is going to get expensive quickly. 😉
Ideally, this would work on both our build server (which has no default printer) and our development machines (which do), so I’d prefer not to change the default printer.
Thanks for all the wonderful answers that will undoubtedly follow this fascinating question.
You should use a mocking framework to mock out the print method so that it is never actually executed.
Check out TypeMock or RhinoMocks for something that will help you do this.
This is how it would look using TypeMock:
Then you would execute the code in your test that inside it calls this method. This will then intercept the print call and the document will not be sent to the printer but you can still unit test the business logic around the time of print.