This library leverages Xunit's fixture implementation to bring dependency injection to .net core test projects powered by Xunit.
Simply create your own fixture and derive from DIFixture
abstract class found in SimcoAI.Test.DependencyInjection
namespace. The following example illustrates it:
public class MyTestFixture : DIFixture
{
protected override void AddServices(IServiceCollection services)
{
services.Configure<MyOptions>(option => Configuration.GetSection("MyOptions").Bind(option));
services.AddSingleton<IMyService, MyServiceImplementation>();
services.AddScoped<IAnotherService, AnotherServiceImplementation>();
}
protected override string GetConfigurationFile()
=> "appsettings.json";
}
MyOptions
, IMyService
, IAnotherService
, MyServiceImplementation
and AnotherServiceImplementation
are interfaces and classes that we assumed they have pre-existed in this example.
The following example illustrates how to assign a test collection to the fixture defined in the previous step:
[CollectionDefinition("my tests")]
public class MyTestCollection : ICollectionFixture<MyTestFixture>
{
}
Your test classes should derive from SimcoeAI.Test.TestsWithFixture
class and it must be decorated by the right collection name. Example:
[CollectionDefinition("my tests")]
public class MyTestCollection : ICollectionFixture<MyTestFixture>
{
}