Skip to content

Commit

Permalink
fix: Postpone exception in DbContainerFixture to match the behavior…
Browse files Browse the repository at this point in the history
… of `ContainerFixture` (#1310)

Co-authored-by: Andre Hofmeister <9199345+HofmeisterAn@users.noreply.github.com>
  • Loading branch information
0xced and HofmeisterAn authored Dec 1, 2024
1 parent 1f78b90 commit e982134
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 6 deletions.
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

0 comments on commit e982134

Please sign in to comment.