How can I check if the value returned as part of that OkObjectResult has a count of 2 without changing any code in the controller action?
Here is my controller action
public IActionResult GetUserNames()
{
var users = _repository.GetUsers();
return Ok(users.Select(u => u.Name));
}
My unit test looks like this
[Fact]
public void GetUserNames_ValidRequest_ShouldReturnOk()
{
_repository
.Setup(r => r.GetUsers())
.Return(new List<User>
{
new User { Name = "SomeRandomName" },
new User { Name = "SomeRandomName2" }
});
var result = _controller.GetUserNames();
result.Should().BeOfType<OkObjectResult>();
// Code to check if 2 names are returned
}
I am using Mock and FluentAssertions in my unit test.
You should be able to do something like this:
var objectResult = Assert.IsType<OkObjectResult>(result);
var model = Assert.IsAssignableFrom<List<string>>(objectResult.Value);
Assert.Equal(2, model.Count);
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