[WebApplicationFactory - TestContainers] PostgreSqlContainer is not initialized. #256
-
Hello everyone, The error it throws is: ErrorSystem.InvalidOperationException
Could not find resource 'PostgreSqlContainer'. Please create the resource by calling StartAsync(CancellationToken) or CreateAsync(CancellationToken). However, test container is initialized in the factory class using IAsyncLifetime and InitiliazeAsync(). StatusControllerFactory.cs
CustomerStatusSteps.cs
Is there any workaround? This implementation works for xUnit runner... |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 4 replies
-
The Maybe others will provide a better idea, but what comes to my mind is that you can initialize your service from an async BeforeScenario hook. Something like this:
If you do so, the |
Beta Was this translation helpful? Give feedback.
-
Hi, for now I use InitializeAsync in constructor hack (and Dispose hack) - I keep my finger crossed and I have hope that IAsyncLifetime will be properly processed in Reqnroll so I will just remove few lines ;) public sealed class DatabaseWithGuidIdFixture
: IAsyncLifetime,
IDisposable
{
private const string DbName = "WithoutNormalizedAspNetIdentityGuid";
private readonly PostgreSqlContainer _postgreSqlContainer
= new PostgreSqlBuilder()
.WithImage("postgres:16.4")
.WithDatabase(DbName)
.WithUsername("admin")
.WithPassword("TestPass123!")
.WithPortBinding(64320, 5432)
.Build();
public DatabaseWithGuidIdFixture()
{
#pragma warning disable VSTHRD002 // Avoid problematic synchronous waits
InitializeAsync().GetAwaiter().GetResult();
#pragma warning restore VSTHRD002 // Avoid problematic synchronous waits
}
public string ConnectionString { get; set; } = string.Empty;
public TestOutputHelperAdapter TestOutputHelperAdapter { get; } = new();
public async Task InitializeAsync()
{
try
{
await _postgreSqlContainer.StartAsync();
var path = Path.Combine("Scripts", "WithoutNormalizedAspNetIdentityGuid.sql");
#pragma warning disable SCS0018
var content = await File.ReadAllTextAsync(path);
#pragma warning restore SCS0018
ConnectionString = _postgreSqlContainer.GetConnectionString();
var upgradeEngineBuilder = DeployChanges.To
.PostgresqlDatabase(ConnectionString, "public")
.WithScript(
"Script_000001_Init", content)
.LogTo(TestOutputHelperAdapter);
var upgradeEngine = upgradeEngineBuilder.Build();
var result = upgradeEngine.PerformUpgrade();
var msg = result.Successful
? "Successfully ran migrations"
: $"Failed to run migrations {result.Error}";
TestOutputHelperAdapter.WriteInformation($"final {msg}");
}
catch (Exception ex)
{
Console.WriteLine(ex);
throw;
}
}
public Task DisposeAsync() => _postgreSqlContainer.DisposeAsync().AsTask();
#pragma warning disable VSTHRD002 // Avoid problematic synchronous waits
public void Dispose() => DisposeAsync().GetAwaiter().GetResult();
#pragma warning restore VSTHRD002 // Avoid problematic synchronous waits
} [CollectionDefinition(nameof(DatabaseWithGuidIdCollection))]
public class DatabaseWithGuidIdCollection
: ICollectionFixture<DatabaseWithGuidIdFixture>
{
// This class has no code, and is never created. Its purpose is simply
// to be the place to apply [CollectionDefinition] and all the
// ICollectionFixture<> interfaces.
// READ MORE: https://xunit.net/docs/shared-context
} WithoutNormalizedAspNetIdentityGuidUserStore.feature Feature: WithoutNormalizedAspNetIdentityGuidUserStore
A short summary of the feature
@xunit:collection(DatabaseWithGuidIdCollection)
Scenario: Create a new user without normalized and Guid id
Given I have configured Identity Connection Provider without normalized and Guid id
When I add the user without normalized and Guid id to the user store
Then the user without normalized and Guid id should be in the user store |
Beta Was this translation helpful? Give feedback.
-
I wanted to kindly drop this here as well. It is an example I created for a different question that was asked here https://github.com/ajeckmans/ReqnrollIntegrationTests , but it matches your problem as well. |
Beta Was this translation helpful? Give feedback.
The
IClassFixture<StatusControllerFactory>
is an xUnit interface and it is not processed / handled by Reqnroll for binding classes.Maybe others will provide a better idea, but what comes to my mind is that you can initialize your service from an async BeforeScenario hook. Something like this: