简体   繁体   English

VS UnitTesting一个FileContentResult-测试时可以生成文件吗

[英]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.

相关问题 当另一个文件发生变化时,如何自动生成文件? - How can i generate a file automatically when another file changes? Delphi 5可以生成VS可以使用的.PDB文件吗? - Can Delphi 5 generate a .PDB file that VS can use? 无法在VS2013中创建测试,没有Microsoft.VisualStudio.TestTools.UnitTesting命名空间 - Can't create test in VS2013, no Microsoft.VisualStudio.TestTools.UnitTesting namespace 我们如何从VS.NET生成构建文件 - How can we generate the build file from VS.NET VS 2010 C# 文件没有错误,但是当我调试时说它找不到神秘文件 - VS 2010 C# file has no errors but when I debug says it can't find a mystery file 如何在GAC中安装Microsoft.Data.Tools.Schema.Sql.UnitTesting.dll - How can i install Microsoft.Data.Tools.Schema.Sql.UnitTesting.dll in GAC 我们不能在VS检测文件编码时如何? - How can VS detect file encoding when we can't? VS负载测试:删除文件测试用例 - VS Load testing: delete file test case 如何从Windows服务文件生成exe - how can i generate exe from windows service file 我可以使用.scmp文件自动生成更改脚本吗? - Can I automatically generate a change script using a .scmp file?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM