.NET Core C# web API integration testing an inmemory database

Soluble Snake 1 Reputation point
2021-06-29T10:50:32.257+00:00

I'm currently trying to create an integration test for my .NET Core Web API. I'm following the advice here: https://timdeschryver.dev/blog/how-to-test-your-csharp-web-api

When I run my test to check the HttpStatusCode 'Should' 'Be' ok, the test is failing BUT the test does pass if I use the '/weatherforecast' example that comes with the .NET Core Web API template. If I use my project, the test fails. Is this because I have no values created in the DB when I start the app? How do I create those values? The code for my test is below:

        [Fact]
public async Task Get_Should_Retrieve_Score()
{
    var response = await Client.GetAsync("/diceroll");
    response.StatusCode.Should().Be(HttpStatusCode.OK);

    //var DiceRoll = JsonConvert.DeserializeObject<DiceRoll[]>(await response.Content.ReadAsStringAsync());
    //forecast.Should().HaveCount(0);
}

Thanks for any help anyone can provide!

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
2,826 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
7,554 questions
No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Michael Taylor 41,191 Reputation points
    2021-06-29T13:53:26.53+00:00

    You didn't post the code for your API so that we can confirm you're returning a 200 from the request. Have you verified via curl, Postman, Powershell or even the browser that the response is actually 200?