'inject RavenTestDriver in my xunit to my main code in .netcore
I have this repository in my project that interacts with raven DB:
public class RavenRepository<TEntity> : IRepository<TEntity>, IDisposable
where TEntity : BaseEntity
{
protected readonly IAsyncDocumentSession _session;
public RavenRepository(IAsyncDocumentSession session)
{
_session = session;
}
//other methods like add search edit update
}
Ass you can see my constructor expects an IAsyncDocumentSession .I want to use RavenTestDriver in my test.So I have to pass RavenTestDriver Session to this class .Here is my test code :
public class TestHostBuilder: RavenTestDriver, IAsyncLifetime
{
protected override void PreInitialize(IDocumentStore documentStore)
{
documentStore.Conventions.MaxNumberOfRequestsPerSession = 50;
ConfigureServer(new TestServerOptions
{
//DataDirectory = "C:\\RavenDBTestDir",
CommandLineArgs = new System.Collections.Generic.List<string> { "--RunInMemory=true", },
FrameworkVersion = null,
});
}
public HttpClient _httpClient = null!;
public async Task InitializeAsync()
{
var store = GetDocumentStore();
var session= store.OpenAsyncSession();
var hostBuilder = easy.api.Program.CreateHostBuilder(new string[0])
.ConfigureWebHost(webHostBuilder =>
{
webHostBuilder.UseTestServer();
})
.ConfigureServices(services =>
{
services.AddScoped<ICurrentUserService, InitRequest>();
services.AddScoped<IAsyncDocumentSession, ?????)>();
});
var host = hostBuilder.Start();
_httpClient = host.GetTestClient();
}
public Task DisposeAsync()
{
return Task.CompletedTask;
}
}
As you can see I create a store and session using ravenTestDriver but I don't know how can I inject this session to IAsyncDocumentSession
Updated
I added this code but again my code uses the main database not raventestdriver why ?
services.AddSingleton<IAsyncDocumentSession>((c) =>
{
return GetDocumentStore().OpenAsyncSession(new SessionOptions()
{
Database="test-server"
});
});
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
