对具有依赖项的类进行单元测试

时间:2019-09-17 09:08:59

标签: c# unit-testing xunit

我有一个需要对其进行一些测试的类,这是非常简单的方法。我的问题是,我要测试的类依赖于其他类,但是我不确定如何解决它。这是一个VSTO应用程序,因此工作表的类型为Tools.Excel。 我要测试的课程:

    public List<ConfigModel> Instances { get; set; }
    public DataReader1 dr;
    public Worksheet sheet;
    public PopulateConfigModel()
    {
        Instances = new List<ConfigModel>();
        dr = new DataReader1();
        sheet = dr.GetWorksheetByName("Your_Data_Sheet");
    }

我的DataReaders ctor看起来像这样

    Workbook workbook;
    public DataReader1()
    {
        workbook = Globals.Factory.GetVstoObject(Globals.ThisAddIn.Application.ActiveWorkbook);
    }

引发NullReferenceExpection。现在,我想知道我的解决方案是紧密耦合还是正常,是否需要在测试中创建解决方法?如果可以,那么我将如何继续呢?

这是我失败的测试方法:

    [Theory]
    [InlineData("B6", 9)]
    [InlineData("B18", 21)]
    [InlineData("-12B17", 1220)]
    [InlineData("/%39F#%(]0€e3€d", 3906)]
    public void FindFirstRowOfDataInlineTest(string startCell, int expected)
    {
        //Arrange
        var PCM = new PopulateConfigModel();
        //Act
        var actual = PCM.FindFirstRowOfData(startCell);

        //Assert
        Assert.Equal(expected, actual);
    }

这是我要测试的方法:

public int FindFirstRowOfData(string startCell)
    {
        string row = "";
        foreach (Char c in startCell)
        {
            if (Char.IsDigit(c))
            {
                row += c;
            }
        }
        //The 3 comes from the fact that there's three extra lines from our headline to our actual data
        int firstRowOfData = Convert.ToInt32(row) + 3;
        return Convert.ToInt32(firstRowOfData);
    }

0 个答案:

没有答案