Sample code for unit testing api controllers

后端 未结 4 871
遥遥无期
遥遥无期 2021-02-04 03:44

Is there an sample code that shows unit testing a controller that inherits from the api controller? I am trying to unit test a POST but it is failing. I believe I need to set up

4条回答
  •  清歌不尽
    2021-02-04 04:21

    Using AutoFixture, I usually do something like this:

    [Theory, AutoCatalogData]
    public void PostToCollectionReturnsCorrectResponse(
        CategoriesController sut,
        CategoryRendition categoryRendition)
    {
        HttpResponseMessage response = sut.Post(categoryRendition);
    
        Assert.Equal(HttpStatusCode.Created, response.StatusCode);
    }
    

    See this other SO answer for more details about this approach.

提交回复
热议问题