您如何测试列表是否在单元测试中得到排序?

How do you test that a list gets ordered in a unit test?

我有一个需要测试的方法需要 IEnumerable<T>。要求此方法按特定顺序针对此可枚举执行一组操作。

(为了简洁起见,我简化了函数。)

public void TestMe(IEnumerable<IObject> objects) {
  foreach (var obj in objects.OrderBy(x => x.Priority)) {
    DoSomethingWithObject(obj);
    DoSomethingElseWithObject(obj);
  }
}

private DoSomethingWithObject(IObject obj) { }
private DoSomethingElseWithObject(IObject obj) { }

如何编写测试以确保在 DoSomethingWithObjectDoSomethingElseWithObject 是私有的情况下以正确的顺序执行操作?

TestMe的结果是什么?调用 TestMe 时调用者会看到什么?为什么他会首先调用这个方法? 他想要完成什么?

这些是您可能希望通过单元测试来回答的问题。实现细节(如私有方法)无关紧要。重要的是外界如何看待你的方法

如果您的方法更改了 IObject 的某些状态,请测试这些更改是否 实际上 已完成。就这么简单。