I have a controller that uses Newtonsoft.Json to convert IEnumerable into JSON format.
[Route("")]
public IHttpActionResult Get()
{
IEnumerable<Product> productList = ProductService.GetAllProducts();
if (!productList.Any())
return Ok();
return Json(productList, new JsonSerializerSettings
{
ContractResolver = new WebContractResolver(),
Converters = new List<JsonConverter> { new TrimStringDataConverter() }
});
}
When I hit the API end point through POSTMAN, it gives me the expected JSON data.
[
{
"code": "prod101",
"title": "LAPTOP"
},
{
"code": "prod102",
"title": "MOBILE"
}
]
Now, I am writing unit test(NUnit) for the controller and I want to get this JSON formatted data in my unit test method. I am able to get the IEnumerable -
IHttpActionResult actionResult = mockProductControllerClient.Get();
JsonResult<IEnumerable<Product>> contentResult = actionResult as JsonResult<IEnumerable<Product>>;
IEnumerable<Product> data = contentResult.Content;
I need the exact same data in Unit Test methods as being received in POSTMAN i.e. JSON data