I am writing an unit test for a mvc web application that checks if a returned list of anonymous variables(in a jsonresult) is correct. therefore i need to iterate through that list but i cannot seem to find a way to do so.
so i have 2 methods
1) returns a json result . In that json result there is a property called data. that property is of type object but internally it’s a list of anonymous variables
2) the method calls method 1 and checks if the returned jsonresult is ok.
if i run the test and i break the debugger i can hover over the result and see the items in it. i just don’t find a way to do so in code.(just using a foreach isn’t possible because at the point i need it i’m not in the method that created the anonymous method)
I think you mean ‘anonymous type’ everywhere you’ve said ‘anonymous variable’ – but you can still iterate over the list with
foreach, just declaring the iteration variable as typeobject:If you need to check the contents, you can use the fact that anonymous types override
ToStringin a useful way. Your test can check the result of projecting each element to a string. Indeed, you could convert your result object to a sequence of strings quite easily:Then test
stringspossibly usingSequenceEqual.