1

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");
    }
}

1 Answer 1

0

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?

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.