tags:

views:

494

answers:

2

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.

+1  A: 

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:

Mock mock = MockManager.Mock<PrintManager>();
mock.ExpectCall("Print");

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.

Campbell
Thankfully, we have TypeMock and this solution will definitely work. However, I'm still curious if there's a Windows equivalent of /dev/null.
Josh Kodroff
+3  A: 

I do this by creating a Windows printer that prints to the NUL device. This is pretty straightforward though the specifics differ a bit between Windows versions: just create a local printer and specify NUL as the local port, "Generic" as the Manufacturer and "Generic / Text Only" as the printer model.

Joe
Brilliant, saved me a lot of dicking about :)
Lunatik