SpecFlow plugin for using Unity as a dependency injection framework for step definitions
C# F# Cucumber Batchfile
Latest commit 5df0c95 Nov 10, 2016 @phatcher First release
Permalink
Failed to load latest commit information.
code First release Nov 11, 2016
nuget First release Nov 11, 2016
sample/MyCalculator First release Nov 11, 2016
.gitattributes First release Nov 11, 2016
.gitignore First release Nov 11, 2016
License.md First release Nov 11, 2016
README.md First release Nov 11, 2016
RELEASE_NOTES.md First release Nov 11, 2016
SpecFlow.Unity.sln First release Nov 11, 2016
build.cmd First release Nov 11, 2016
build.fsx First release Nov 11, 2016

README.md

SpecFlow Unity

SpecFlow plugin for using Autofac as a dependency injection framework for step definitions.

Currently supports

  • SpecFlow v2.1
  • Unity v4.0.1 or above

Available as a NuGet packages SpecFlow.Unity

NuGet Build status

Welcome to contributions from anyone.

There's a sample project at https://github.com/phatcher/SpecFlow.Unity/tree/master/sample/MyCalculator

You can see the version history here.

Build the project

  • Windows: Run build.cmd

I have my tools in C:\Tools so I use build.cmd Default tools=C:\Tools encoding=UTF-8

Library License

The library is available under the MIT License, for more information see the License file in the GitHub repository.

Getting Started

This project was inspired by SpecFlow.Autofac and follows the similar patterns as that project and the associated blog post http://gasparnagy.com/2016/08/specflow-tips-customizing-dependency-injection-with-autofac/

Install plugin from NuGet into your SpecFlow project.

PM> Install-Package SpecFlow.Unity

Create a static method somewhere in the SpecFlow project (recommended to put it into the Support folder) that returns a Unity IUnityContainer 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 public types from the SpecFlow project:

container.RegisterAssemblyTypes(typeof(YourClassInTheSpecFlowProject).Assembly, WithMappings.FromMatchingInterface, WithName.Default, WithLifetime.ContainerControlled);

or by registering all classes marked with the [Binding] attribute:

builder.RegisterTypes(typeof(TestDependencies).Assembly.GetTypes().Where(t => Attribute.IsDefined(t, typeof(BindingAttribute))), WithMappings.FromMatchingInterface, WithName.Default, WithLifetime.ContainerControlled);

A typical container creation method probably looks like this:

[ScenarioDependencies]
public static IUnityContainer CreateContainer()
{
  // create container with the runtime dependencies
  var container = Dependencies.CreateContainer();

  //TODO: add customizations, stubs required for testing

  // Registers the build steps, this gives us dependency resolution using the container.
  // NB If you need named parameters into the steps you should override specific registrations
  container.RegisterTypes(typeof(TestDependencies).Assembly.GetTypes().Where(t => Attribute.IsDefined(t, typeof(BindingAttribute))), WithMappings.FromMatchingInterface, WithName.Default, WithLifetime.ContainerControlled);

  return container;
}

Also don't forget to modify your test App.config to refer to the plugin

<specFlow>
    <!-- For additional details on SpecFlow configuration options see http://go.specflow.org/doc-config -->
    <unitTestProvider name="NUnit" />
    <runtime stopAtFirstError="false" missingOrPendingStepsOutcome="Inconclusive" />
    <plugins>
        <add name="SpecFlow.Unity" type="Runtime" />
    </plugins>
</specFlow>

You must also have at least one step definition in the same assembly as the ScenarioDependencies method for the it to be found.