Assert.AreEqual方法在检查DataTable类型时的用法

本文关键字:类型 用法 DataTable 检查 AreEqual 方法 Assert | 更新日期: 2023-09-27 18:28:27

我已经浏览了给定的线程,但在c#中找不到传递用例的实现(msdn网站也没有数据表/泛型集合断言用法)。。

客户端的代码不在我们身边(只有2个数据表返回给我们)

datarow dr1 = datatable1.newrow();
dr1[0] = 345;
datarow dr2 = datatable1.newrow()
dr2[0] = 345;
datatable1.rows.add(dr1); //(1)
datatable2.rows.add(dr2); //(2)

我们端的代码

Assert.AreEqual(dt1,dt2); //fails!!How to pass this case??

Assert.AreEqual方法在检查DataTable类型时的用法

Assert.AreEqual将对类型使用Equals方法。DataTable不会覆盖此方法,因此这是一个参考检查。这意味着dt1不等于dt2

您可以使用CollectionAssert,但这将对成员进行比较。同样,DataRow不会覆盖Equals,因此它将进行引用检查。

您需要编写自定义比较逻辑,然后执行类似Assert.IsTrue(DataTableComparer.AreEqual(dt1, dt2)); 的操作

AreEqual方法的版本使用Assert.AreEqual(Object expected, Object actual)。这意味着它将使用Object.Equals()进行比较。

运行此代码并查看其值:

bool areEqual = dr1.Equals(dr2);

这将返回false,因为它们不是同一个引用。查看Object.Equals:的文档

如果当前实例是引用类型,则Equals(Object)方法测试引用相等性,并且对Equals的调用等效于对ReferenceEquals方法的调用。引用相等意味着被比较的对象变量引用相同的对象。

您需要找到一种更合适的方法来比较您的数据。您可以使用DataRowComparer来比较这些值。您还可以循环浏览每一行,并自己比较这些值。有关示例,请参见dotnetperls。

我在开发自己的CSV解析库时遇到了类似的问题。我写了一个断言,它以一种可以使用的方式抛出异常单元内测试。

public static class AssertDataTable {
    public static void AreEqual(DataTable expected, DataTable actual) {
        //If both tables are null we consider them equal.
        if ((expected == null) && (actual == null)) {
            return;
        } else if ((expected != null) && (actual != null)) {
            //Check that the column count is the same
            if ((expected.Columns.Count == actual.Columns.Count)) {
                //Check that the column types are the same
                for (int i = 0; i < expected.Columns.Count; i++) {
                    if (expected.Columns[i].DataType != actual.Columns[i].DataType) {
                        throw new Exception($"The type of of columns is not the same in both tables! Expected:{expected.Columns[i].DataType} Actual:{actual.Columns[i].DataType} for index {i}");
                    }
                }
                //Check that the headers are the same
                for (int i = 0; i < expected.Columns.Count; i++) {
                    if (expected.Columns[i].ColumnName != actual.Columns[i].ColumnName) {
                        throw new Exception($"Column names do not match! Expected:{expected.Columns[i].ColumnName} Actual:{actual.Columns[i].ColumnName} for index {i}");
                    }
                }
            } else {
                throw new Exception("Tables do not have the same number of columns");
            }
            //Up to this point we have verified the columns. Now we are going th verify the rows
            //Just verify the values. Types have already been verified through the column checks.
            if (expected.Rows.Count == actual.Rows.Count) {
                for (int i = 0; i < expected.Columns.Count; i++) {
                    for (int j = 0; j < expected.Rows.Count; j++) {
                        if (!expected.Rows[j][i].Equals(actual.Rows[j][i])) {
                            throw new Exception($"Cells are not equal! In Row {j} and Column {i} Expected {expected.Rows[j][i]} Actual {actual.Rows[j][i]}");
                        }
                    }
                }
                return;
            } else {
                throw new Exception($"Rows count is not equal! Expected{expected.Rows.Count} Actual {actual.Rows.Count}");
            }
        } else {
            //Give more information to the user.
            if (expected == null) {
                throw new Exception("Expected table is null! But Actual table is not.");
            }
            if (actual == null) {
                throw new Exception("Actual table is null! But expected table is not.");
            }
        }
    } //End of Compare Data Datatables
} //End of class

我还考虑了Servy和Gun2171的建议。该代码也可以在相关的Github Repo 上找到

通过这种方式,我可以在单元测试中使用这种方法,如下所示:

[TestMethod]
public void SingleTest() {
    //Arrange
    DataTable expected = new DataTable();
    //Build my table here
    
    //Act
    DataTable actual = CustomMethodToTest();
    //Assert
    AssertDataTable.AreEqual(expected, actual); 
}

我发布这篇文章是希望这篇文章能在未来帮助到任何人。

AreEqual通过引用进行比较,除非对象重写"Equals"方法

请尝试以下粘贴自http://social.msdn.microsoft.com/Forums/vstudio/en-US/23703a85-20c7-4759-806a-fabf4e9f5be6/how-to-compare-two-datatables

Assert.IsTrue(ComareDataTables(DTable1,DTable2));
private bool CompareDataTables(DataTable DT1, DataTable DT2)
    {
        if ((DT1 == null) && (DT2 == null))
            return true;
        else if ((DT1 != null) && (DT2 != null))
        {
            if (DT1.Rows.Count == DT2.Rows.Count)
            {
                if (DT1.Columns.Count == DT2.Columns.Count)
                {
                    for (int i = 0; i < DT1.Rows.Count; i++)
                    {
                        for(int j = 0; j<DT1.Columns.Count; j++)
                        {
                            if (DT1.Rows[i][j].ToString() != DT2.Rows[i][j].ToString())
                                return false;
                        }
                    }
                    return true;
                }
                else
                    return false;
            }
            else
                return false;
        }
        else
            return false;
    }