C#,Web应用程序。 testingExcel文件生成
在许多C#.NET Web应用程序中,用户需要将数据下载为Excel文件。
创build/pipe理Excel电子表格我使用: EPPlus 。
要打开Excel文件,我使用和推荐: Microsoft.Office.Interop.Excel 。
在一个MVC的应用程序中,我可以下载Excel文件:
public ActionResult DownloadExcell() { byte[] report = _codeManager.CreateExcel(); return BinaryResponseReport(report); } private ActionResult BinaryResponseReport(byte[] report) { Response.Clear(); Response.Buffer = true; Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; Response.AddHeader("content-disposition", "attachment;filename=FooBar.xlsx"); Response.BinaryWrite(report); Response.End(); return RedirectToAction("Index"); }
但是,当testing_codeManager.CreateExcel()
函数时,我不想_codeManager.CreateExcel()
Web应用程序的所有步骤,以获得Excell的下载function。
所以在我的UnitTest项目的解决scheme。 我添加一个TestMethod:
[TestMethod] public void TestExcellGeneration() { byte[] report = _codeManager.CreateExcel(); OpenExcelFromTempFile(excelFileBytes); } private void OpenExcelFromTempFile(byte[] data) { string tempPath = System.IO.Path.GetTempFileName(); System.IO.File.WriteAllBytes(tempPath, data); Application excelApplication = new Application(); _Workbook excelWorkbook; excelWorkbook = excelApplication.Workbooks.Open(tempPath); excelApplication.Visible = true; }
运行这个unit testing将excell写入TempFile。
我的问题:我应该添加这样的“unit testing”到我的解决scheme吗? 有没有其他更好的方法来testing这个function?
因为我实际上不是以Arrange
方式Assert
方式testingfunction。