简体   繁体   English

比较两个DataTables(单元测试,集成测试,C#,TestMethod)

[英]Comparing two DataTables (Unit Testing, Integration Testing, C#, TestMethod)

What kind of tests should be carried out in unit testing when comparing Datatables that are supposed to be different and have multiple rows. 在比较应该是不同的并且具有多行的数据表时,应该在单元测试中执行什么样的测试。

    [TestMethod]
    public void ExecuteOutWithMultipleDataTables()
    {
        //Arrange
        int id1 = TestOrderBuilder.New().Build();
        DataTable dtDefault = CreateDefaultDataTable(id1, "OUT", "TableDesc", DateTime.Now);

        //Act
        object[] result = OracleDatabase.ExecuteOut(SqlStatements.Cursor, procedureParameters);
        DataTable dtResults = result[0] as DataTable;

        //Assert
        Assert.IsNotNull(dtDefault);
        Assert.IsNotNull(dtResults);
        Assert.AreNotEqual(dtDefault, dtResults);
        Assert.AreNotSame(dtDefault.Rows[0][0], dtResults.Rows[0][0]);
        Assert.AreNotSame(dtDefault.Rows[0][1], dtResults.Rows[0][1]);
    }

This is an example of some of what I have wrote already, but I am unsure if I am on the right track. 这是我已经写过的一些例子,但我不确定我是否在正确的轨道上。

Does anyone have advice? 有人有建议吗?

Mac 苹果电脑

You need to write a helper method if you need to verify each row and column value. 如果需要验证每个行和列值,则需要编写辅助方法。

Also , it does not looks like an unit test because it looks you are calling real database rather then mocking it. 此外,它看起来不像单元测试,因为它看起来你正在调用真正的数据库,而不是嘲笑它。

may be something like below 可能是下面的东西

private bool IsTableSame(DataTable t1, DataTable t2)
    {
        if (t1 == null)
            return false;
        if (t2 == null)
            return false;
        if (t1.Rows.Count != t2.Rows.Count)
            return false;

        if (t1.Columns.Count != t2.Columns.Count)
            return false;

        if (t1.Columns.Cast<DataColumn>().Any(dc => !t2.Columns.Contains(dc.ColumnName)))
        {
            return false;
        }

        for (int i = 0; i <= t1.Rows.Count-1; i++)
        {
            if (t1.Columns.Cast<DataColumn>().Any(dc1 => t1.Rows[i][dc1.ColumnName].ToString() != t2.Rows[i][dc1.ColumnName].ToString()))
            {
                return false;
            } 
        }

        return true;
    }

I have wrapped the same helper method above to use asserts. 我已经包含了上面的相同帮助方法来使用断言。 It helps in debugging a unit test. 它有助于调试单元测试。

private void AssertTableRecordsAreEqual(DataTable expectedTable, DataTable actualTable)
    {
        Assert.IsNotNull(actualTable, "Table is empty");
        Assert.AreEqual(expectedTable.Columns.Count, actualTable.Columns.Count, "Number of columns in actual and expected tables are different");
        Assert.AreEqual(expectedTable.Rows.Count, actualTable.Rows.Count, "Number of records in actual and expected tables are different");
        Assert.IsFalse(expectedTable.Columns.Cast<DataColumn>().Any(dc => !actualTable.Columns.Contains(dc.ColumnName)), "Table column names are different");

        for (int i = 0; i <= expectedTable.Rows.Count - 1; i++)
        {
            Assert.IsFalse(expectedTable.Columns.Cast<DataColumn>().Any(dc1 => expectedTable.Rows[i][dc1.ColumnName].ToString() != actualTable.Rows[i][dc1.ColumnName].ToString()), "Table row value is different");
        }
    }

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

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