I have an application that sends out many PDF’s to a printer. Has anyone had any experience creating a Mock object that represents a local printer?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Not entirely sure what you are trying to do, but this might help.
In order to mock a printer (or any other external device) you should encapsulate all the calls to the printer behind an interface, e.g.
All your other code must then talk to the printer through this interface.
You can then implement one version of this interface that talks to the real printer, and one fake object that you can use when testing etc.
The fake object can easily be mocked using a mocking framework like Rhino Mocks or Moq, or you can just implement a fake one yourself.
Update:
All the classes that uses the printer will then look something like this:
BTW, if you uses a IoC container then you don’t need the first constructor. You then inject the printer classes using the IoC tool.