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
While unit testing, how can I
Assert that the content is as I expect it? For example, the following assertion fails
var contentResult = response as OkNegotiatedContentResult<IEnumerable<dynamic>>;
Assert.IsNotNull(contentResult);
Reduce this IEnumerable<dynamic>
result to an IEnumerable<long>
so I can verify that it contains the expected sequence of values?
I have already added the InternalsVisibleTo
attribute to the AssemblyInfo.
1. Something to start with:
response.GetType().GetGenericTypeDefinition() == typeof(OkNegotiatedContentResult<>)
You can continue type investigation from here if you want.
2. The solution for second point is pretty easy:
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();
If the tests are in separate assembly - add [assembly: InternalsVisibleTo("TestAssembly")]
as anonymous types are generated as internal.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With