SpecFlow plugin that enables to use Microsoft.Extensions.DependencyInjection for resolving test dependencies.
Currently supports:
Based on https://github.com/gasparnagy/SpecFlow.Autofac (now part of SpecFlow).
Install plugin from NuGet into your SpecFlow project.
PM> Install-Package SolidToken.SpecFlow.DependencyInjection
Create a static method somewhere in the SpecFlow project (recommended to put it into the Support
folder) that returns an Microsoft.Extensions.DependencyInjection IServiceCollection
and tag it with the [ScenarioDependencies]
attribute. Configure your dependencies for the scenario execution within the method. You also have to register the step definition classes, that you can do by either registering all classes marked with the [Binding]
attribute:
foreach (var type in typeof(TestDependencies).Assembly.GetTypes().Where(t => Attribute.IsDefined(t, typeof(BindingAttribute))))
{
services.AddSingleton(type);
}
A typical dependency builder method probably looks like this:
[ScenarioDependencies]
public static IServiceCollection CreateServices()
{
var services = new ServiceCollection();
// TODO: add customizations, stubs required for testing
foreach (var type in typeof(TestDependencies).Assembly.GetTypes().Where(t => Attribute.IsDefined(t, typeof(BindingAttribute))))
{
services.AddSingleton(type);
}
return services;
}
Refer to SpecFlow.DependencyInjection.Tests
for a typical implementation.