.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!

Developer technologies | ASP.NET | ASP.NET Core
Developer technologies | C#
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Michael Taylor 60,341 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?

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.