1

i am new to integration tests. I have an xUnit project in my solution which contains one test only. Here's the definition of my test:

[Fact]
public async Task ShouldCreateUser()
{
    // Arrange
    var createUserRequest = new CreateUserRequest
    {
        Login = "testowyLogin",
        Password = "testoweHaslo",
        FirstName = "testoweImie",
        LastName = "testoweNazwisko",
        MailAddress = "test@test.pl"
    };
    var serializedCreateUserRequest = SerializeObject(createUserRequest);
    
    // Act
    var response = await HttpClient.PostAsync(ApiRoutes.CreateUserAsyncRoute,
        serializedCreateUserRequest);
    
    // Assert
    response
        .StatusCode
        .Should()
        .Be(HttpStatusCode.OK);
}

And the BaseIntegrationTest class definition:

public abstract class BaseIntegrationTest
{
    private const string TestDatabaseName = "TestDatabase";
    
    protected BaseIntegrationTest()
    {
        var appFactory = new WebApplicationFactory<Startup>()
            .WithWebHostBuilder(builder =>
            {
                builder.ConfigureServices(services =>
                {
                    RemoveDatabaseContextFromServicesCollectionIfFound<EventStoreContext>(services);
                    RemoveDatabaseContextFromServicesCollectionIfFound<GrantContext>(services);
                    
                    services
                        .AddDbContext<EventStoreContext>(options =>
                            options.UseInMemoryDatabase(TestDatabaseName))
                        .AddDbContext<GrantContext>(options =>
                            options.UseInMemoryDatabase(TestDatabaseName));
                });
            });
        
        HttpClient = appFactory.CreateClient();
    }

    protected HttpClient HttpClient { get; }
    
    protected static StringContent SerializeObject(object @object) =>
        new StringContent(
            JsonConvert.SerializeObject(@object),
            Encoding.UTF8,
            "application/json");

    private static void RemoveDatabaseContextFromServicesCollectionIfFound<T>(IServiceCollection services)
        where T : DbContext
    {
        var descriptor = services.SingleOrDefault(service =>
            service.ServiceType == typeof(DbContextOptions<T>));

        if (!(descriptor is null))
        {
            services
                .Remove(descriptor);
        }
    }
}

When i run tests, it takes few seconds, and the test ends successfully. The problem is that Resharper Test Runner still runs, although i've already have collected results. what am i doing wrong here? Do i have to somehow dispose the HttpClient, after performing all tests? If so, how to achieve that? Thanks for any help. enter image description here

Bulchsu
  • 580
  • 11
  • 33

1 Answers1

0

It looks like you're actually booting the application inside the test rather than using the testhost (https://learn.microsoft.com/en-us/aspnet/core/test/integration-tests?view=aspnetcore-3.1)

public class BasicTests 
    : IClassFixture<WebApplicationFactory<RazorPagesProject.Startup>>
{
    private readonly WebApplicationFactory<RazorPagesProject.Startup> _factory;

    public BasicTests(WebApplicationFactory<RazorPagesProject.Startup> factory)
    {
        _factory = factory;
    }

    [Theory]
    [InlineData("/")]
    [InlineData("/Index")]
    [InlineData("/About")]
    [InlineData("/Privacy")]
    [InlineData("/Contact")]
    public async Task Get_EndpointsReturnSuccessAndCorrectContentType(string url)
    {
        // Arrange
        var client = _factory.CreateClient();

        // Act
        var response = await client.GetAsync(url);

        // Assert
        response.EnsureSuccessStatusCode(); // Status Code 200-299
        Assert.Equal("text/html; charset=utf-8", 
            response.Content.Headers.ContentType.ToString());
    }
}

Notice the IClassFixture stuff.

RubbleFord
  • 7,456
  • 9
  • 50
  • 80