0

I am unit testing code that depends on iTextSharp open source Pdf library. One of the classes inTextSharp is PdfReader with one of the constructors that accepts byte array. I simplified the problem to the following:

    [TestMethod]
    [HostType("Moles")]
    public void ReadPdf()
    {
        MPdfReader.ConstructorByteArray = (@this, pdfIn) =>
        {
            new MPdfReader(@this)
            {                    
            };
        };

        PdfReader reader = new PdfReader(new byte[] { 10, 20, 30 });
    }

However, this code still calls the real PdfReader and not the mock:

iTextSharp.text.pdf.PdfReader.CheckPdfHeader iTextSharp.text.pdf.PdfReader.ReadPdf() iTextSharp.text.pdf.PdfReader..ctor(Byte[] pdfIn, Byte[] ownerPassword) iTextSharp.text.pdf.PdfReader..ctor(Byte[] pdfIn)

and not surprisedly, it blows up with "..System.IO.IOException: PDF header signature not found.. "

Not sure what I'm doing wrong....

-Stan

StanB
  • 1
  • 1

1 Answers1

0

I assume you are attempting to defuse the constructor call that accepts a byte array. Try removing the instance parameter in your constructor overload:

[TestMethod]
[HostType("Moles")]
public void ReadPdf()
{
    MPdfReader.ConstructorByteArray = (@this, pdfIn) =>
    {
        new MPdfReader()
        {
        };
    };
    PdfReader reader = new PdfReader(new byte[] { 10, 20, 30 });
}
Mike Christian
  • 1,526
  • 1
  • 15
  • 27