簡體   English   中英

VS UnitTesting一個FileContentResult-測試時可以生成文件嗎

[英]VS UnitTesting a FileContentResult - Can I generate the file when testing

我想對返回PDF文件的FileContentResult的方法進行單元測試。 我正在做一些測試來檢查內容類型和文件名,但是如果我能以某種方式生成PDF作為測試的一部分,那將是很棒的嗎? 我目前有一種方法,如下所示:

    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");
        }
    }

我正在測試以下內容:

    [TestMethod]
    public void ReturnFileContentResult()
    {
        DocumentPDFPrinterController pdfController = new DocumentPDFPrinterController();
        FileContentResult result = pdfController.ConvertToPDF(0);

        Assert.IsTrue(result.ContentType == "application/pdf" && result.FileDownloadName.Contains(".pdf"));
    }

我可以在此文本中添加任何內容,以在給定位置創建pdf文件(用戶下載嗎?)。

在提出問題之前,應該做更多的調查。 我發現只需添加以下內容即可為我完成工作:

System.IO.File.WriteAllBytes(@“ C:\\ Users \\ username \\ Downloads \\ Test.pdf”,result.FileContents);

那很好地生成了我的文件。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM