NUnit comparing two lists

后端 未结 6 535
孤独总比滥情好
孤独总比滥情好 2021-01-01 10:16

OK so I\'m fairly new to unit testing and everything is going well until now. I\'m simplifying my problem here, but basically I have the following:

[Test]
pu         


        
6条回答
  •  太阳男子
    2021-01-01 11:00

    If you can't modify a class then this example can be helpful:

    [Test]
    public void Arrays_Should_Be_Equal()
    {
        MyClass[] array1 = GetTestArrayOfSize(10);
        MyClass[] array2 = GetTestArrayOfSize(10);
    
        // DOESN'T PASS
        // Assert.That(array1, Is.EquivalentTo(array2));
    
        Func selector = i => new { i.Property1, i.Property2 };
        Assert.That(array1.Select(selector), Is.EquivalentTo(array2.Select(selector)));
    }
    
    private MyClass[] GetTestArrayOfSize(int count)
    {
        return Enumerable.Range(1, count)
            .Select(i => new MyClass { Property1 = "Property1" + i, Property2 = "Property2" + i }).ToArray();
    }
    

提交回复
热议问题