samedi 23 juillet 2016

Unit test for a collection of anonymous JSON objects

This question was inspired by this excellent example. I have ASP.NET Core MVC application and I am writing unit tests for the controller. One of the methods returns JsonResult collection. I can get to each element of the collection. I can also assert values in each element like this:

Dictionary<int, string> expectedValues = new Dictionary<int, string> {
    { 1, "Welcome Tester"},
    { 2, "Namaste Tester"},
    { 3, "Privet Tester"},
    { 4, "Labdien Tester"}
};
foreach (dynamic value in jsonCollection) {
    dynamic json = new DynamicObjectResultValue(value);
    Assert.Equal(expectedValues[json.Id], json.Greeting);
}

But is there a way to make assertions on the whole collection? For example, Assert.Equal(4, jsonCollection.Count()) or Assert.Contains(2, jsonCollection[Id] (this is obviously pseudo-code).

Aucun commentaire:

Enregistrer un commentaire