I have written this controller method and this test.
Controller method:
public async Task<IActionResult> Metric(string type, string source)
{
// Check existence ...
var model = await _context
.Metrics
.FirstAsync(mt => mt.Type == metricType.AsInt() && mt.Source == source);
Response.StatusCode = HttpStatusCode.OK.AsInt();
return View(model);
}
Test:
[Fact]
public async Task MetricExistsTest()
{
// Arrange ...
// Act
var result = await _controller.Metric(Metrics.CpuLoad.ToString(), "source-1");
// Assert
var viewResult = Assert.IsType<ViewResult>(result);
Assert.Equal(HttpStatusCode.OK.AsInt(), viewResult.StatusCode.Value);
var model = Assert.IsAssignableFrom<Metric>(
viewResult.ViewData.Model
);
}
Now, the problem is here Assert.Equal(HttpStatusCode.OK.AsInt(), viewResult.StatusCode.Value);
. The viewResult.StatusCode
is indeed null
. If I comment that line out, everything works.
What am I doing wrong? Why is it null
? Do I properly set Response.StatusCode
? How do I verify status code then?
Thank you!