Automated Testing OpenXML SDK

前端 未结 4 454
無奈伤痛
無奈伤痛 2021-01-02 06:40

I\'m implementing ms word document generation using content controls and OpenXML SDK. I\'d like to have some automated testing for that code (unit tests or some easy UI auto

4条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-02 07:33

    I am actually doing something similar with the OpenXML SDK for spreadsheets and I actually just write OpenXML api code that opens the file from a stream for the purpose of testing. Unit Tests don't really tell you enough since you need to know if it is a valid file.

    // There should be a sheet for every team
    [TestMethod]
    [HostType("Moles")]
    public void CaseExportTeamSheetsTest()
    {
        IRepository ServiceRepository;
        CaseController target;
        BuildCaseControllerMoledCases(out ServiceRepository, out target);
        FileStreamResult actual = target.Export();   using (SpreadsheetDocument spreadsheetDocument = SpreadsheetDocument.Open(actual.FileStream, false))
        {
            var services = ServiceRepository.All;
    
            foreach (var item in services)
            {
                // get a worksheet foreach service
                var sheets = spreadsheetDocument.WorkbookPart.Workbook.Descendants().Where(s => s.Name == item.ServiceName);
                Assert.IsTrue(sheets.Count() > 0);
            }
        }
    
        actual.FileStream.Close();
        actual.FileStream.Dispose();
    }
    

提交回复
热议问题