A colleague asked me to take a look at the following code inside a test project:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Assert.Collection(list, | |
item => item.Description.Contains("ABC"), | |
); |
My first guess would be that this code checks that the specified condition(the contains) is true for every element in the list.
This turns out not to be the case. The Assert.Collection expects a list of element inspectors, one for every item in the list. The first inspector is used to check the first item, the second inspector the second item and so on. The number of inspectors should match the number of elements in the list.
An example:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var list = new List<int> { 42, 2112 }; | |
Assert.Collection(list, | |
item => Assert.Equal(42, item), | |
item => Assert.Equal(2112, item) | |
); |
The behavior I expected could be achieved using the Assert.All method:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Assert.All(list, | |
item => Assert.Contains(i.Description, "ABC") | |
); |