In my Web API I have a simple controller action:
[HttpGet]
[Route("[action]")]
public IActionResult GetApiStatus(string token)
{
if (_token != token)
{
return StatusCode(401);
}
StatusModel status = _vesselUpdating.GetUpdatingStatus();
return Ok(status);
}
And action returns StatusModel
type object. I could not manage how to test its return status.
My test method is below:
[Theory]
[InlineData("wrong_token", StatusCodes.Status401Unauthorized)]
[InlineData("test_token", StatusCodes.Status200OK)]
[InlineData("", StatusCodes.Status401Unauthorized)]
[InlineData(null, StatusCodes.Status401Unauthorized)]
public void Get_GetAnyAction_WithVariusTokens_ReturnsCorrectStatusCode(string token, int code)
{
_vesselUpdatingMock.Setup(v => v.GetUpdatingStatus()).Returns(_sampleStatusModel);
IActionResult result;
result = _updatesController.GetApiStatus(token);
StatusCodeResult resultGetApiStatus = result as StatusCodeResult;
Assert.Equal(code, resultGetApiStatus.StatusCode);
}
When debugging on
result = _updatesController.GetApiStatus(token);
the StatusCode
property of IActionResult
is 200, but only DeclaredType
property of that is null.
And when Asserting, at the end I have in my resultGetApiStatus
null.
I did not managed to find similar example. How can I project result
properly?
IMPORTANT EDIT:
I found only this approach, that does not fix my problem, regarding variety of cases [Theory]
, and not all my other actions returns ObjectResult
, some of them return only StatusCode
. Is there any universal approach for actions with and without ObjectResult
? Or should I use different tests?