Is there an easy way in xunit.net to compare two collections without regarding the items' order?

后端 未结 5 510
借酒劲吻你
借酒劲吻你 2021-01-01 11:08

In one of my tests, I want to ensure that a collection has certain items. Therefore, I want to compare this collection with the items of an expected collection not r

5条回答
  •  梦毁少年i
    2021-01-01 11:49

    This is almost the same as your code. The only simplification is using Assert.Contains instead of Assert.True(expected.Contains(...)).

    [Fact]
    public void SomeTest()
    {
        // Do something in Arrange and Act phase to obtain a collection
        List actual = ...
    
        // Now the important stuff in the Assert phase
        var expected = new List { 42, 87, 30 };
        Assert.Equal(expected.Count, actual.Count);
        foreach (var item in expected)
            Assert.Contains(item, actual);
    }
    

提交回复
热议问题