JustMock and System.Drawing.Printing.PrintDocument

2 posts, 1 answers
  1. Hesham Desouky
    Hesham Desouky avatar
    29 posts
    Member since:
    Oct 2012

    Posted 03 Dec 2012 Link to this post

    I tried to use JustMock Replace technique to replace the Print method of  System.Drawing.Printing.PrintDocument.

    In class Initialize
    Mock.Replace<System.Drawing.Printing.PrintDocument>(p => p.Print()).In<PrintJobTest>(t => t.Ensure_Print_Job_Folder_Name());

    and in my unit test method arranged the PrintDocument.Print as follows:

    var printDocument = new System.Drawing.Printing.PrintDocument();
    Mock.Arrange(() => printDocument.Print()).IgnoreInstance().DoNothing();

    When PrintDocument object instatiated under the class to be tested, it throws the following exception when calling the PrintDocument.Print method

    Could not load file or assembly 'Telerik.CodeWeaver.Api, Version=, Culture=neutral, PublicKeyToken=87210992829a189d' or one of its dependencies. The system cannot find the file specified.

    This error occurs when running the test using MSTest runner, but if I used JustCode test runner everything run ok and no exception thrown

    And no errors in the event viewer regarding the profiler:
    .NET Runtime version 4.0.30319.17929 - The profiler was loaded successfully.  Profiler CLSID: '{b7abe522-a68f-44f2-925b-81e7488e9ec0}'.  Process ID (decimal): 6368.  Message ID: [0x2507].

    Is this a problem whenmocking the System.Drawing.Printing.PrintDocument?

    Please help I am facing many troubles with JustMock and my unit tests using MSTest runner.

    If this a test runner problem, do you suggest a replacement?
  2. Answer
    Kaloyan avatar
    880 posts

    Posted 05 Dec 2012 Link to this post

    Hello Hesham,

    Thank you for bringing this issue to us.

    You do not need to use Mock.Replace in order to mock the "System.Drawing.Printing.PrintDocument" class. Mock.Replace is used for mocking MSCorlib members, you could read more about it here.

    To demonstrate you how the PrintDocument class could be faked, I made the following example class:
    public class MyClass
        public void PrintingDocument(System.Drawing.Printing.PrintDocument doc)

    Now, in our test method I will check if the doc.Print() is actually been called. I`m also arranging that it should be called exactly once, like this:
    public void TestMethod1()
        var mock = Mock.Create<System.Drawing.Printing.PrintDocument>();
        var newMyClassInstance = new MyClass();
        bool isCalled = false;
        // Arrange
        Mock.Arrange(() => mock.Print()).DoInstead(() => isCalled = true).OccursOnce();
        // Act
        // Assert

    Note that, I`m using MSTesting framework and the test is behaving as expected.

    I must object that, our product is well integrated in working with MSTest runner. Still, I could also recommend you using JustCode testing framework, NUnit or TestDriven.NET.

    You are always welcome to check our online help documentation, in which you will be able to find much more information and examples concerning JustMock.

    I hope this answers your questions. Just ask, if there is anything else I could help you with.

    the Telerik team
    Share what you think about JustTrace & JustMock with us, so we can become even better! You can use the built-in feedback tool inside JustTrace, our forums, or our JustTrace or JustMock portals.
Back to Top