Skip to content

AdCodicem's SpecFlow plugin for Microsoft.Extensions.DependencyInjection.

License

Notifications You must be signed in to change notification settings

mbhoek/SpecFlowMicrosoftDependencyInjection

 
 

Repository files navigation

⚠️ This repo is not maintained anymore, please use SpecFlow.DependencyInjection ⚠️

AdCodicem.SpecFlow.MicrosoftDependencyInjection

AdCodicem.SpecFlow.MicrosoftDependencyInjection is a SpecFlow plugin that enables to use Microsoft.Extensions.DependencyInjection for resolving test dependencies. It's based on Gáspár Nagy's SpecFlow.Autofac plugin.

Currently supports :

  • SpecFlow 3.7 or above
  • Microsoft.Extensions.DependencyInjection 2.1 or above

License: Apache

NuGet: Nuget

Build Status Build Status

Release Status Release Status

Why this plugin ?

I created this plugin for Specflow because most of my projects use the plain Microsoft.Extensions.DependencyInjection for injecting dependency. If you've already familiar why ASP.Net Core dependency injection, you won't be lost with this plugin 😉.

Usage

Typical usage

Install plugin from NuGet into your SpecFlow project.

PM> Install-Package AdCodicem.SpecFlow.MicrosoftDependencyInjection

Create a non-static class somewhere in the SpecFlow project implementing the IServicesConfigurator interface. Configure the dependencies within the method.

You don't have to register the step definition classes as it's already done by this plugin.

⚠️ Do not use other dependency injection plugin for SpecFlow.

A typical dependency configuration class probably looks like this :

public class Startup : IServicesConfigurator
{
    /// <inheritdoc />
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddTransient<ICalculator, Calculator>();
    }
}

Delegate the resolution to the original container

Since v1.1.0 you can instruct the service collection to use the dependencies registered automatically by Specflow.

public class Startup : IServicesConfigurator
{
    /// <inheritdoc />
    public void ConfigureServices(IServiceCollection services)
    {
        services
            // ITestOutputHelper is automatically registered by Specflow when using xUnit runner
            .AddDelegated<ITestOutputHelper>()
            // Calculator can use ITestOutputHelper as it's registered above to delegate the resolution to the original container
            .AddTransient<ICalculator, Calculator>();
    }
}

To register a delegated service, simply add the following call in configuration pipeline: AddDelegated<TService>(). The service resolution is delegated to the original container used by Specflow.

Release History

v1.1.3

  • Fix: Support for Specflow v3.7+

v1.1.1

  • Fix: Added Intellisense documentation

v1.1.0

  • Feature: Allow the delegation of the resolution of some services to the original Specflow dependency injection. The delegated services must be registered like this: services.AddDelegated<ITestOutputHelper>().

v1.0.2

  • Fix: Registered services are now disposed if they're disposable.

v1.0.1

  • Initial release.
  • Increased minimal Microsoft.Extensions.DependencyInjection version to 2.1.0 to prevent MissingMethodException issue when using an higher version of the NuGet.

About

AdCodicem's SpecFlow plugin for Microsoft.Extensions.DependencyInjection.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • C# 100.0%