0

I have gone through threads given but wasn't able to find an implementation for pass case in c# (msdn website has no datatable/generic collection assert usage either)..

code at client not with us (only the 2 datatables are returned to us)

datarow dr1 = datatable1.newrow();
dr1[0] = 345;

datarow dr2 = datatable1.newrow()
dr2[0] = 345;

datatable1.rows.add(dr1); //(1)
datatable2.rows.add(dr2); //(2)

code at our end

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

4 回答 4

4

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

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

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

于 2013-08-21T19:48:20.980 回答
1

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

运行这段代码,看看值是什么:

bool areEqual = dr1.Equals(dr2);

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

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

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

于 2013-08-21T19:47:49.337 回答
0

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;
    }
于 2013-08-21T19:50:54.727 回答
0

我在开发自己的 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

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

这样我可以在单元测试中使用这个方法,如下所示:

[TestMethod]
public void SingleTest() {
    //Arrange
    DataTable expected = new DataTable();
    //Build my table here
    
    //Act
    DataTable actual = CustomMethodToTest();

    //Assert
    AssertDataTable.AreEqual(expected, actual); 
}

我发布这个是希望这篇文章将来可以帮助任何人。

于 2022-01-31T21:24:22.967 回答