C
C#2y ago
dave1

Registering IDbCommand and autowiring and testing

i've registered the following
builder.Services.AddScoped<IDbConnection>(
_ => new NpgsqlConnection(builder.Configuration.GetConnectionString("DefaultConnection"))
);
builder.Services.AddScoped<RegionRepository>();
builder.Services.AddScoped<IDbConnection>(
_ => new NpgsqlConnection(builder.Configuration.GetConnectionString("DefaultConnection"))
);
builder.Services.AddScoped<RegionRepository>();
i now want to change that in my CustomWebApplicationFactory like..
var conn = services.FirstOrDefault(descriptor => descriptor.ServiceType == typeof(IDbConnection));
if (conn is null)
{
throw new Exception("NpgsqlConnection is not registered.");
}
services.Remove(conn);
services.AddScoped<IDbConnection>(
_ => new NpgsqlConnection(_postgreSqlContainer.GetConnectionString())
);
var conn = services.FirstOrDefault(descriptor => descriptor.ServiceType == typeof(IDbConnection));
if (conn is null)
{
throw new Exception("NpgsqlConnection is not registered.");
}
services.Remove(conn);
services.AddScoped<IDbConnection>(
_ => new NpgsqlConnection(_postgreSqlContainer.GetConnectionString())
);
but doing this the 'regions' table cannot be found, i'd presume it's not registering it correctly. When not swapping the implementation, it finds my local db with my local appsettings connection string the table does exist because i'm also registering AppDbContext with entity framework(and those tests are passing, i'm currently converting everything to repositories)
1 Reply
dave1
dave1OP2y ago
Foolishly didnt grt useLowerCaseColumnNames in my test startup So when using raw sql it was incorrect
Want results from more Discord servers?
Add your server