Sto provando a configurare il mio ambiente di test, ma ho problemi con l'adattatore Sqlite. Ogni contesto creato ha la stessa connessione, quindi il databse in memoria dovrebbe essere costruito correttamente per ogni contesto.
Ma quando sto provando ad aggiungere qualcosa di nuovo, si genera un errore: "Nessuna tabella:% here_is_my_tablename%".
Penso che la mia configurazione dovrebbe essere buona.
Base:
public abstract class BaseServiceTests : IDisposable
{
protected readonly SqliteConnection Connection;
public BaseServiceTests()
{
Connection = new SqliteConnection("DataSource=:memory:");
Connection.Open();
Assert.NotNull(Connection);
using (var ctx = BuildCoreContext())
{
ctx.Database.EnsureCreated();
}
using (var ctx = BuildWebContext())
{
ctx.Database.EnsureCreated();
}
}
public void Dispose()
{
Connection.Close();
}
public DbContextOptions<TContext> Options<TContext>() where TContext: DbContext
{
var options = new DbContextOptionsBuilder<TContext>()
.UseSqlite(Connection)
.Options;
return options;
}
public ServiceRequestCoreContext BuildCoreContext()
{
var ctx = new ServiceRequestCoreContext(Options<ServiceRequestCoreContext>(), null);
ctx.Database.OpenConnection();
return ctx;
}
public ServiceRequestWebContext BuildWebContext()
{
var ctx = new ServiceRequestWebContext(Options<ServiceRequestWebContext>(), null);
ctx.Database.OpenConnection();
return ctx;
}
}
Test
public class RequestServiceTests : BaseServiceTests
{
public async Task Prepare()
{
using (var ctx = BuildCoreContext())
{
await ctx.RequestTypes.AddAsync(new RequestType((int)RequestTypes.Order, "TestType - test"));
await ctx.RequestStatuses.AddAsync(new RequestStatus((int)RequestStatuses.AcceptedForVeryfication, "test - test", "test - test"));
await ctx.Companies.AddAsync(new CustomerCompany(1, "test - test", "Test - test"));
await ctx.SaveChangesAsync();
}
}
[Fact]
public async Task when_creating_new_request_it_should_not_be_null()
{
//Arrange
await Prepare();
var customerId = 1;
var iGen = new IdentifyGenerator();
//Act
using (var webCtx = BuildWebContext())
{
webCtx.Database.OpenConnection();
var service = new RequestService(webCtx, BuildCoreContext(), iGen);
await service.CreateAsync(customerId);
await webCtx.SaveChangesAsync();
}
//Assert
using (var ctx = BuildWebContext())
{
ctx.ServiceRequests.Should().HaveCount(1);
ctx.ServiceRequests.FirstOrDefault().Should().NotBeNull();
}
}
}
Per rispondere a OP, credo che il problema sia rappresentato da più contesti che accedono al database, che non funziona senza una cache condivisa. Modificare la stringa di connessione in: "DataSource=file::memory:?cache=shared"
.
Per coloro che vagano qui da Google, ecco alcune altre cose da tenere a mente:
string connectionString = "DataSource=file::memory:?cache=shared";
System.Data.SQLite.SQLiteConnection
(pacchetto di nuget System.Data.SQLite.Core) al provider Microsoft.Data.Sqlite.SqliteConnection
: Microsoft.Data.Sqlite.SqliteConnection
quindi gli errori sono scomparsi. Codice di esempio:
[Fact]
public async Task MyTest()
{
var dbContext = new MyDbContext("DataSource=file:memdb1?mode=memory&cache=shared");
try
{
await dbContext.Database.OpenConnectionAsync();
// Test your dbContext methods, which has an open connection to the in-memory database
// If using ADO.NET to make new connections, use Microsoft.Data.Sqlite.SqliteConnection
}
finally
{
await dbContext.Database.CloseConnectionAsync();
}
}
Ricorda anche di leggere la documentazione ! :)