How to Unit Test with ActionResult?

后端 未结 3 835
-上瘾入骨i
-上瘾入骨i 2020-12-18 19:06

I have a xUnit test like:

[Fact]
public async void GetLocationsCountAsync_WhenCalled_ReturnsLocationsCount()
{
    _locationsService.Setup(s => s.GetLocat         


        
3条回答
  •  余生分开走
    2020-12-18 19:38

    The issue is wrapping it in Ok. If you return the object itself, Value is populated correctly.

    If you look at Microsoft's examples in the docs, they only use the controller methods for non-default responses like NotFound:

    [HttpGet("{id}")]
    [ProducesResponseType(StatusCodes.Status404NotFound)]
    public ActionResult GetById(int id)
    {
        if (!_repository.TryGetProduct(id, out var product))
        {
            return NotFound();
        }
    
        return product;
    }
    

提交回复
热议问题