Search code examples
c#owinxunitfluent-interfacekatana

Getting 404 Not Found only when re-creating database in tests for 2nd API call


I'm trying to create API integration tests for legacy code I've inherited.

Currently I have a piece of testing code that:

  1. recreate database (using Fluent Migrations)
  2. starts a web app (Owin.Hosting)
  3. makes api call to get auth token
  4. makes api call to authorized endpoint

It works perfectly if I skip first step and do only 2) 3) 4).

It's a bit weird that I'm also able to do steps 1) 2) 3) (so the auth API call works with database recreation included).

I thought my web api isn't working properly, but I'm able to do basic path when I don't recreate database. Then I thought maybe it's not working at all when I recreate database, but I'm able to authorize an user. I have no clues what could I try now.

[Collection("Database Create collection")]
public class RoleControllerTests : IDisposable
{
    private readonly IDisposable _server;
    private readonly string _url = new Configuration().ServerUrl;

    public RoleControllerTests()
    {
        _server = WebApp.Start<Startup>(_url);
    }

    public void Dispose()
    {
        _server.Dispose();
    }

    [Fact]
    public async Task basic_roles_should_exist_in_the_database()
    {
        // Arrange
        var roleApi = RestClient.For<IRoleController>(_url);
        IAuthorize auth = new Authorize();
        roleApi.AuthenticationHeader = await auth.GetAuthenticationHeaderAsync();

        // Act
        var rolesData = await roleApi.List();

        // Assert
        rolesData.ShouldContain(x => x.Name == "User");
        rolesData.ShouldContain(x => x.Name == "Displayer");
    }
}

Solution

  • So I've changed testing framework to NUnit and it's working. I have no idea why, does XUnit have some issues with changing things in runtime?