We are trying the Test Driven Development using Fluent Assertions for our .Net core 3.1 Web API and using XUnit for the same.
This is what my controller returns.
{
"usersResult": [
{
"UserId": "1",
"UserName": "Foo"
},
{
"UserId": "2",
"UserName": "Boo"
}
]
}
In my test method, I want to check if it returns an object, i.e. I want to assert on the userResult Type, when I debug its showing anonymous type for "userResult" , so I am confused to what type should I specify in : Should().BeOfType(??)
[HttpGet]
public async Task<IActionResult> GetUsers()
{
Users us = new Users();
var us = await _service.GetUsers();
return Ok(new { usersResult = us });
}
public class Users
{
public string UserId{ get; set; }
public string UserName{ get; set; }
}
// TestMethod :
[Fact]
public async Task GetUsers_OnSuccess_ReturnsListOfUsers()
{
var sut = new UserController();
var result = await sut.GetUsers();
result.Should().BeOfType<OkObjectResult>();
var objectResult = (OkObjectResult)result;
objectResult.Value.Should().BeOfType<**WHAT_To_Specify**>();
}