Let's say the return type is
public ActionResult Balance()
{
var myDtos = new List<BalanceModelDto>();
return Ok(new {data = myDtos});
}
and in my xunit, I assert the return type.
using (var client = new HttpClient())
{
var response = await client.GetAsync($"/Balance");
var responseString = await response.Content.ReadAsStringAsync();
var result = JsonConvert.DeserializeObject<BalanceModelDto>(responseString);
Assert.IsType<BalanceModelDto>(result);
Assert.Equal(1, result.Balance);
}
The problem is responseString
is "{\"data\":[{"balance\":1}]}"
. But what I want is just "[{"balance\":1}]"
CodePudding user response:
Either: you should be looking at result.data
instead of just result
.
or: return Ok(myDtos);
from the ActionMethod.
CodePudding user response:
Deserialize into JObject
:
var responseRaw = (JObject) JsonConvert.DeserializeObject(responseString);
Then convert "data" property of that json object into BalanceModelDto[]
:
var result = responseRaw["data"].ToObject<BalanceModelDto[]>();