[英]VS UnitTesting a FileContentResult - Can I generate the file when testing
I want to Unit Test a method that returns a FileContentResult of a PDF file. 我想对返回PDF文件的FileContentResult的方法进行单元测试。 I am doing some tests to check the content type & file name, but it would be great if I could also generate the PDF somehow as part of the test?
我正在做一些测试来检查内容类型和文件名,但是如果我能以某种方式生成PDF作为测试的一部分,那将是很棒的吗? I currently have a method as follows:
我目前有一种方法,如下所示:
public FileContentResult ConvertToPDF(int baseClassId)
{
try
{
return new DocumentWriter().ConvertDocumentToPDFSharp(baseClassId);
}
catch (Exception ex)
{
Logger.Instance.LogError("Error in Assessments_FormHeaderController ConvertToPDF", ex);
return new FileContentResult(GetBytes("Error fetching pdf, " + ex.Message + Environment.NewLine + ex.StackTrace), "text/plain");
}
}
and I am testing with the following: 我正在测试以下内容:
[TestMethod]
public void ReturnFileContentResult()
{
DocumentPDFPrinterController pdfController = new DocumentPDFPrinterController();
FileContentResult result = pdfController.ConvertToPDF(0);
Assert.IsTrue(result.ContentType == "application/pdf" && result.FileDownloadName.Contains(".pdf"));
}
Can I add anything to this text which will create the pdf file in a given location(user downloads?). 我可以在此文本中添加任何内容,以在给定位置创建pdf文件(用户下载吗?)。
Should have done some more investigating before putting the question out there. 在提出问题之前,应该做更多的调查。 I have found simply adding the following got the job done for me:
我发现只需添加以下内容即可为我完成工作:
System.IO.File.WriteAllBytes(@"C:\\Users\\username\\Downloads\\Test.pdf", result.FileContents); System.IO.File.WriteAllBytes(@“ C:\\ Users \\ username \\ Downloads \\ Test.pdf”,result.FileContents);
That generates my file nicely. 那很好地生成了我的文件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.