How to debug a printer in .NET?

I have an application that sends a lot of PDF to a printer. Has anyone had experience creating a Mock object that is a local printer?

+5
source share
3 answers

Not quite sure what you are trying to do, but it may help.

To mock a printer (or any other external device), you must encapsulate all calls to the printer behind the interface, for example.

interface IPrinter 
{
   void Print(PrintData data);
}

Then all other codes should talk to the printer through this interface.

Then you can implement one version of this interface that speaks to a real printer, and one fake object that you can use for testing, etc.

, , Rhino Mocks Moq, .

public class FakePrinter : IPrinter
{
   public void Print(PrintData data)
   {
      // Write to output window or something
   }
}

Update:

, , :

public class ClassThatPrints
{
   private IPrinter _Printer;

   // Constructor used in production
   public ClassThatPrints() : this(new RealPrinter())
   {
   }

   // Constructor used when testing
   public ClassThatPrints(IPrinter printer)
   {
      _Printer = printer;
   }

   public void MethodThatPrints()
   {
      ...
      _Printer.Print(printData)
   }
}

BTW, IoC, . IoC.

+12

.

, .

. ​​

0

- LPD 9100 Windows, "" 9100 515 (LPD).

0

All Articles