Open samba2 opened 9 months ago
@javiercn @riande can you help with this?
I've come up with a workable solution until Microsoft gives better guidance. I've posted it on the SO post you linked to.
@ShawnTheBeachy just had a first brief look. Thank you for this detailed response 👍 I will dig into it as soon as possible.
@ShawnTheBeachy There is one problem with this approach, you did not solve and we did. Problem is descibed as following: what if we want to use two or more Worker services, each of which has of course its own appsettings.json file. But current approach do not perform change of its ContentRootPath to assembly source root directory (project directory), but it takes it from Executable directory, which of course never has both files of appsettings.json from both worker services, because they have same names.
Indeed WebApplicationFactory has its own solution which based on directory upper traversal from assembly executable directory and then assign it to content root of builder. We have really smart solution.
private static string GetThisFilePath([CallerFilePath] string path = "")
{
return path;
}
private static HostApplicationBuilder CreateHostBuilder(HostApplicationBuilderSettings settings)
{
var path = GetThisFilePath();
var builder = GetApplicationHostBuilder(settings, path);
}
public static HostApplicationBuilder GetApplicationHostBuilder(HostApplicationBuilderSettings settings, string path)
{
var directory = Path.GetDirectoryName(path);
if (settings != null)
settings.ContentRootPath = directory;
return Host.CreateApplicationBuilder(settings);
}
Note private static string GetThisFilePath([CallerFilePath] string path = "") method should be placed in Program.cs file to get desired effect
URL
https://learn.microsoft.com/en-us/aspnet/core/test/integration-tests?view=aspnetcore-7.0
Operating system
Windows
More information about your system
No response
Description
The integration testing tutorial contains a very detailed description of how to run integration tests for web apps using the WebApplicationFactory.
However, I have a worker service which I want to test in the same way. How do I do that?
Example
Given this worker service:
I want to be able to write an end-to-end test which provides data source and sink via testcontainers (works) and the unit under test, the worker service, somehow directly via .NET Core. With an web application you would use the WebApplicationFactory. I am looking for a way to do the same with a Worker service.
Here is the test I'd like to be able to write:
I've got a similar version of this test already working by running the SamplerWorker also as a Docker container. However, I want to be able to easily debug my unit under test which gets really complicated with my current Docker approach.
How can directly start the worker service in my test ? Thank you for your support!
fyi: I've also asked this question on Stackoverflow.
dotnet --info
No response