I have a class that implements DocRaptor's pdf creator
public class PdfCreator {
public byte[] createPdf(string html){
int tryCount = 3;
while(tryCount > 0) {
try {
Configuration.Default.Username = "YOUR_API_KEY_HERE"; // this key works for test documents
DocApi docraptor = new DocApi();
Doc doc = new Doc(
// create a new doc object
);
AsyncDoc response = docraptor.CreateAsyncDoc(doc);
AsyncDocStatus status_response;
Boolean done = false;
while(!done) {
// Mocked this but it's getting ovewritten with a different StatusId
status_response = docraptor.GetAsyncDocStatus(response.StatusId);
Console.WriteLine("doc status: " + status_response.Status);
switch(status_response.Status) {
case "completed":
done = true;
byte[] doc_response = docraptor.GetAsyncDoc(status_response.DownloadId);
File.WriteAllBytes("/tmp/docraptor-csharp.pdf", doc_response);
Console.WriteLine("Wrote PDF to /tmp/docraptor-csharp.pdf");
break;
case "failed":
done = true;
Console.WriteLine("FAILED");
Console.WriteLine(status_response);
break;
default:
Thread.Sleep(1000);
break;
}
}
} catch (DocRaptor.Client.ApiException error) {
if (--tryCount == 0) throw;
}
}
}
}
}
In my test, I'm trying to force it to fail and throw exception in the catch block by mokcing the GetAsyncDocStatus
call to return a status of failed
_docRaptorService.Setup(p => p.GetAsyncDocStatus(It.IsAny<string>())).Returns("failed");
var docRaptor = new PdfCreator();
string html = $"<html><body><h1>Hello World!</h1></body></html>";
byte[] doc = docRaptor.createPdf(html);
_docRaptorService.Verify(p => p.GetAsyncDocStatus(It.IsAny<string>()), Times.Once);
But when I run the test the test fails saying the method was not called. And if I run the test in debug mode, the GetAsyncDocStatus
returns completed
instead of failed
, which makes me think the class is not using the mocked version of the method. How can I solve this?