Closed michaeltg17 closed 6 months ago
Supported at 9.2.1
See demo
Hi.
I tried it but it only works with services registered in the Startup class or with the ITestOutputHelperAccessor.
I have an scenario in which I need something like this:
public class BeforeAfterTestConfiguration(WebApplicationFactoryFixture webApplicationFactoryFixture, ITestOutputHelper testOutputHelper)
: BeforeAfterTest
{
public override void Before(object? testClassInstance, MethodInfo methodUnderTest)
{
if (testClassInstance is Test test)
{
test.WebApplicationFactoryFixture = webApplicationFactoryFixture;
test.TestOutputHelper = testOutputHelper.Output;
test.Initialize();
}
}
}
These parameters (collection fixture and test output) are injected fine in the current running test class but not in this BeforeAfterTest class.
TestFixture is xunit feature, you must register it as a (keyed) service.
ITestOutputHelper => ITestOutputHelperAccessor
How would you register it? Okay keyed but what lifetime and what key? I'm not able to see how it would work.
In my case the fixture has an IMessageSink parameter as well.
WebApplicationFactoryFixture(IMessageSink messageSink)
Okay with point 2.
services.AddScoped<WebApplicationFactoryFixture>()
, public class BeforeAfterTestConfiguration(WebApplicationFactoryFixture webApplicationFactoryFixture)
or
services.AddKeyedSingleton<WebApplicationFactoryFixture>(KeyedService.AnyKey)
, public class BeforeAfterTestConfiguration([FromKeyedServices("someKey")]WebApplicationFactoryFixture webApplicationFactoryFixture)
Okay, it works perfectly. Thanks!
Usually you have dependencies in the constructor of each test class like this:
But sometimes you only use them in the base class so the ideal code would look like this:
To achive this, having support for property injection would be nice.
In this project you can have it: https://github.com/flavien/quickwire
The problem is that the test class is created outside the DI container and its dependencies passed manually (only constructor's ones).
I think it won't be hard to add support for property injection as the main work is already done.