【问题标题】:How to determine if two List<FileInfo> contain the same elements using MSTest?如何使用 MSTest 确定两个 List<FileInfo> 是否包含相同的元素?
【发布时间】:2013-06-30 09:54:47
【问题描述】:

我试图弄清楚如何确定两个List&lt;FileInfo&gt; 何时不同。为什么 MSTest 告诉我这两个与 CollectionAssert.AreEquivalentSequenceEqual 不同?

测试失败:CollectionAssert.AreEquivalent failed. The expected collection contains 1 occurrence(s) of &lt;C:\Windows\Microsoft.NET\Framework\v3.0\WPF\PenIMC.dll&gt;. The actual collection contains 0 occurrence(s).

    string basePath = Path.Combine(Environment.GetEnvironmentVariable("windir"),
                                   @"Microsoft.NET\Framework\v3.0\WPF");

    var fiList1 = new List<FileInfo> { new FileInfo(Path.Combine(basePath, "PenIMC.dll")) };
    var fiList2 = new List<FileInfo> { new FileInfo(Path.Combine(basePath, "PenIMC.dll"))};

    CollectionAssert.AreEquivalent(fiList1, fiList2); //why is this failing?
    //And this one too...
    Assert.IsTrue(fiList1.SequenceEqual(fiList2));

我可以让它与int 等简单类型一起使用,类似于this,但不适用于我的List&lt;FileInfo&gt;。如果您不能仅通过查看我上面的代码来确定这一点,也许可以提供一种聪明的方法来调查这一点 - 例如将对象序列化为 XML,我可以区分它们。我猜我错过了一些东西。

【问题讨论】:

    标签: c# unit-testing mstest


    【解决方案1】:

    我相信这与引用相等有关。如果您将new FileInfo 存储在同一个变量中,则此方法有效。因此,您必须使用覆盖Equals 方法的复杂类型

    【讨论】:

    • 那么简单地创建我自己的比较器类型来覆盖这样的 eqauls?:stackoverflow.com/questions/6007495/…
    • 如果它不是你可以覆盖的东西,似乎就是这样。
    • 鉴于 CollectionAssert.AreEquival
    猜你喜欢
    • 2010-12-13
    • 2012-09-11
    • 2013-12-28
    • 2012-05-17
    • 2020-02-07
    • 1970-01-01
    • 2023-03-18
    • 2017-02-08
    • 2012-08-21
    相关资源
    最近更新 更多