简体   繁体   English

在OpenXml-SDK中使用TDD

[英]Using TDD with OpenXml-SDK

I have started using a TDD approach to develop a small app that reads data from Excel files. 我已经开始使用TDD方法开发一个从Excel文件中读取数据的小应用程序。 Using a repository pattern type approach I have come to a hurdle which baffles me. 使用存储库模式类型的方法我遇到了困扰我的障碍。

In order to read the Excel files, I am using the OpenXml-SDK. 为了读取Excel文件,我使用的是OpenXml-SDK。 Now typically reading from an Excel file using the SDK requires several if not more steps to actually get the values you want to read. 现在通常使用SDK从Excel文件中读取需要几个(如果不是更多)步骤来实际获取您想要读取的值。

The approach I have taken thus far is reflected in the following test and accompanying function. 到目前为止我所采用的方法反映在以下测试和附带功能中。

    [Test]
    public void GetRateData_ShouldReturn_SpreadSheetDocument()
    {
        //Arrange
        var fpBuilder = new Mock<IDirectoryBuilder>();
        fpBuilder.Setup(fp => fp.FullPath()).Returns(It.IsAny<string>());

        var doc = new Mock<IOpenXmlUtilities>();
        doc.Setup(d => d.OpenReadOnlySpreadSheet(It.IsAny<string>()))
             .Returns(Mock.Of<SpreadsheetDocument>());

        swapData = new SwapRatesRepository(fpBuilder.Object, doc.Object);

        //Act
        var result = swapData.GetRateData();

        //Assert
        doc.Verify();
        fpBuilder.Verify();
    }

public class SwapRatesRepository: IRatesRepository<SwapRates>
{
    private const string SWAP_DATA_FILENAME = "DATE_MKT_ZAR_SWAPFRA1.xlsx";
    private IDirectoryBuilder builder;
    private IOpenXmlUtilities openUtils;

    public SwapRatesRepository(IDirectoryBuilder builder)
    {
        // TODO: Complete member initialization
        this.builder = builder;
    }

    public SwapRatesRepository(IDirectoryBuilder builder, 
                                       IOpenXmlUtilities openUtils)
    {
        // TODO: Complete member initialization
        this.builder = builder;
        this.openUtils = openUtils;
    }

    public SwapRates GetRateData()
    {
        // determine the path of the file based on the date
        builder.FileName = SWAP_DATA_FILENAME;
        var path = builder.FullPath();

        // open the excel file
        using(SpreadsheetDocument doc = openUtils.OpenReadOnlySpreadSheet(path))
        {
            //WorkbookPart wkBookPart = doc.WorkbookPart;
            //WorksheetPart wkSheetPart = wkBookPart.WorksheetParts.First();
            //SheetData sheetData = wkSheetPart.Worksheet
            //                                 .GetFirstChild<SheetData>();

        }

        return new SwapRates(); // ignore this class for now, design later 
    }
}

However, the next steps after the spreadsheet is open would be to actually start interrogating the Excel object model to retrieve the values. 但是,电子表格打开后的下一步将是实际开始询问Excel对象模型以检索值。 As noted above, I making use of mocks for anything open xml related. 如上所述,我使用模拟来打开与xml相关的任何东西。 However, in some cases the objects can't be mocked(or I don't know how to mock them since they are static). 但是,在某些情况下,对象不能被模拟(或者我不知道如何模拟它们,因为它们是静态的)。 That gave rise to IOpenXmlUtilities which are merely simple wrapper calls into the OpenXml-SDK. 这就产生了IOpenXmlUtilities ,这只是对OpenXml-SDK的简单包装调用。

In terms of design, we know that reading data from excel files is a short term solution (6-8 months), so these tests only affect the repository/data access for the moment. 在设计方面,我们知道从excel文件中读取数据是一个短期解决方案(6-8个月),因此这些测试目前仅影响存储库/数据访问。

Obviously I don't want to leave the TDD approach(as tempting as it is), so I am looking for advise and guidance on how to continue my TDD endeavours with the OpenXml SDK. 显然我不想离开TDD方法(虽然很诱人),所以我正在寻找有关如何继续使用OpenXml SDK进行TDD工作的建议和指导。 The other aspect relates to mocking - I am confused as to when and how to use mocks in this case. 另一个方面涉及嘲弄 - 我很困惑在这种情况下何时以及如何使用模拟。 I don't want to unknowingly writes tests that test the OpenXml-SDK. 我不想在不知不觉中编写测试OpenXml-SDK的测试。

*Side note: I know that the SOLIDity of my design can be improved but I leaving that for now. *附注:我知道我的设计的SOLIDity可以改进,但我现在就离开了。 I have a set of separate tests that relate to the builder object. 我有一组与builder对象相关的单独测试。 The other side effect that may occur is the design of an OpenXML-SDK wrapper library. 可能发生的另一个副作用是OpenXML-SDK包装器库的设计。

Edit: Unbeknown at the time, by creating the OpenXML-SDK wrappers for the OpenXML-SDK, i have used a design pattern similar (or exact) called the Adaptor pattern . 编辑:当时不知道,通过为OpenXML-SDK创建OpenXML-SDK包装器,我使用了类似(或精确)的设计模式,称为适配器模式

If you can't mock it and can't create a small unittest, it might be better to take it to a higher level and make a scenario test. 如果您无法模拟它并且无法创建小型单元测试,那么最好将其提升到更高级别并进行方案测试。 You can use the [TestInitialize] and [TestCleanup] methods to create a setup for the test. 您可以使用[TestInitialize]和[TestCleanup]方法为测试创建设置。

using Pex and Moles might be another way to get it tested. 使用Pex和Moles可能是另一种测试方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM