Unit Test for checking IEnumerable <dynamic> content
Given the following WebAPI method:
public IHttpActionResult GetDeliveryTypes()
{
return Ok(... .Select(dt => new { Id = dt.Id, Name = dt.Name }));
}
Where
typeof(Id) = long
typeof(Name) = string
During unit testing, how can I
Claim that the content is what I expect? For example, the following statement fails
var contentResult = response as OkNegotiatedContentResult<IEnumerable<dynamic>>; Assert.IsNotNull(contentResult);Reduce the result
IEnumerable<dynamic>toIEnumerable<long>so that I can make sure that it contains the expected sequence of values?
I already added an attribute InternalsVisibleToto AssemblyInfo.
+4
1 answer
1. Something to start:
response.GetType().GetGenericTypeDefinition() == typeof(OkNegotiatedContentResult<>)
You can continue to research type here if you want.
2. The solution for the second point is quite simple:
dynamic response = controller.GetDeliveryTypes();
Assert.True(response.GetType().GetGenericTypeDefinition() == typeof(OkNegotiatedContentResult<>));
var content = (IEnumerable<dynamic>)response.Content;
var ids = content.Select(i => i.Id).ToList();
- [assembly: InternalsVisibleTo("TestAssembly")], .
+4