Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Postpone exception in DbContainerFixture to match the behavior of ContainerFixture #1310

Merged
merged 2 commits into from
Dec 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Testcontainers.Xunit/DbContainerFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ protected override async LifetimeTask InitializeAsync()
await base.InitializeAsync()
.ConfigureAwait(false);

_testMethods = new DbContainerTestMethods(DbProviderFactory, ConnectionString);
_testMethods = new DbContainerTestMethods(DbProviderFactory, new Lazy<string>(() => ConnectionString));
}

/// <inheritdoc />
Expand Down
2 changes: 1 addition & 1 deletion src/Testcontainers.Xunit/DbContainerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ protected override async LifetimeTask InitializeAsync()
await base.InitializeAsync()
.ConfigureAwait(false);

_testMethods = new DbContainerTestMethods(DbProviderFactory, ConnectionString);
_testMethods = new DbContainerTestMethods(DbProviderFactory, new Lazy<string>(() => ConnectionString));
}

/// <inheritdoc />
Expand Down
8 changes: 4 additions & 4 deletions src/Testcontainers.Xunit/DbContainerTestMethods.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
namespace Testcontainers.Xunit;

internal sealed class DbContainerTestMethods(DbProviderFactory dbProviderFactory, string connectionString) : IDbContainerTestMethods, IAsyncDisposable
internal sealed class DbContainerTestMethods(DbProviderFactory dbProviderFactory, Lazy<string> connectionString) : IDbContainerTestMethods, IAsyncDisposable
{
private readonly DbProviderFactory _dbProviderFactory = dbProviderFactory ?? throw new ArgumentNullException(nameof(dbProviderFactory));
private readonly string _connectionString = connectionString ?? throw new ArgumentNullException(nameof(connectionString));
private readonly Lazy<string> _connectionString = connectionString ?? throw new ArgumentNullException(nameof(connectionString));

#if NET8_0_OR_GREATER
[CanBeNull]
Expand All @@ -12,7 +12,7 @@ private DbDataSource DbDataSource
{
get
{
_dbDataSource ??= _dbProviderFactory.CreateDataSource(_connectionString);
_dbDataSource ??= _dbProviderFactory.CreateDataSource(_connectionString.Value);
return _dbDataSource;
}
}
Expand All @@ -32,7 +32,7 @@ private DbDataSource DbDataSource
public DbConnection CreateConnection()
{
var connection = _dbProviderFactory.CreateConnection() ?? throw new InvalidOperationException($"DbProviderFactory.CreateConnection() returned null for {_dbProviderFactory}");
connection.ConnectionString = _connectionString;
connection.ConnectionString = _connectionString.Value;
return connection;
}

Expand Down