如果使用 Fluent Assertions 的顺序不同,如何断言两个列表不等效

How to assert two lists are not equivalent if in different order using Fluent Assertions

使用 Fluent Assertions 我们可以断言两个集合是相等的(根据 属性 值)使用类似的东西:

list1.ShouldBeEquivalentTo(list2);

假设 list1list2 包含相同的对象 in any order,则断言为真。

如果我们想断言列表的顺序是正确的,我们可以这样做:

list1.ShouldBeEquivalentTo(list2, o => o.WithStrictOrdering());

我正在寻找如果列表包含 wrong order 中的相同对象但我找不到任何断言的东西。

使用 Fluent Assertions 的最佳方式是什么?

PS - 这是一种学术上的好奇心,在现实中可能没有那么大的用处:)

编辑:现在我明白了戴维的要求(见下面的评论)我更新了我的代码到这个解决方案。虽然语法相似,但它不是 FluentAssertion 扩展,但通过一些操作它可能是。

public static class IEnumerableAssertionExtensions
{
    public static void ShouldContainInWrongOrder<TSubject>(this IEnumerable<TSubject> source, IEnumerable<TSubject> expected)
    {
        var remaining = expected.ToList();
        var inOrder = true;
        foreach (var subject in source)
        {
            if (inOrder && !ReferenceEquals(subject, remaining[0]))
            {
                inOrder = false;
            }
            var s = subject;
            Execute.Verification.ForCondition(() => remaining.Remove(s)).FailWith("Expected item in the collection: {0}", subject.ToString());
        }

        Execute.Verification.ForCondition(() => remaining.Count == 0).FailWith(string.Format("{0} more item{1} than expected found in the list.", remaining.Count, ((remaining.Count == 1) ? string.Empty : "s")));
        Execute.Verification.ForCondition(() => !inOrder).FailWith("list items are ordered identically");
    }
}

[TestClass]
public class TestFoo
{
    class Thing
    {
        public int i;
    }

    [TestMethod]
    public void MyMethod()
    {
        var a1 = new Thing { i=0 };
        var a2 = new Thing { i=1 };
        var a3 = new Thing { i=2 };
        var a4 = new Thing { i=2 };
        var list1 = new List<Thing> { a1, a2, a3 };
        var list2 = new List<Thing> { a1, a2, a3 };
        var list3 = new List<Thing> { a3, a2, a1 };
        var list4 = new List<Thing> { a1, a2, a3, a4 };
        var list5 = new List<Thing> { a3, a2 };

        list1.ShouldContainInWrongOrder(list3); // Succeeds
        list1.ShouldContainInWrongOrder(list2); // Fails
        list1.ShouldContainInWrongOrder(list4); // Fails
        list1.ShouldContainInWrongOrder(list5); // Fails
    }
}