Skip to content
This repository was archived by the owner on May 22, 2024. It is now read-only.

Commit dd65e8e

Browse files
authored
Add MissingScenarioDependenciesException and update documentation (#28)
* Update documentation * Update SpecFlow packages to 3.1.86 * Add MissingScenarioDependenciesException
1 parent ec2944b commit dd65e8e

File tree

6 files changed

+28
-10
lines changed

6 files changed

+28
-10
lines changed

README.md

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ Currently supports:
1111
* [SpecFlow v3.0 or above](https://www.nuget.org/packages/SpecFlow/3.0)
1212
* [Microsoft.Extensions.DependencyInjection v2.1 or above](https://www.nuget.org/packages/Microsoft.Extensions.DependencyInjection/2.1)
1313

14+
Listed on [Available Plugins for SpecFlow](https://specflow.org/documentation/Available-Plugins/).
1415
Based on https://github.com/gasparnagy/SpecFlow.Autofac (now [part of SpecFlow](https://github.com/SpecFlowOSS/SpecFlow/tree/master/Plugins/SpecFlow.Autofac.SpecFlowPlugin)).
1516

1617
## Usage
@@ -21,18 +22,23 @@ Install plugin from NuGet into your SpecFlow project.
2122
PM> Install-Package SolidToken.SpecFlow.DependencyInjection
2223
```
2324

24-
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:
25+
Create a static method in your SpecFlow project that returns a `Microsoft.Extensions.DependencyInjection.IServiceCollection` and tag it with the `[ScenarioDependencies]` attribute.
26+
Configure your test dependencies for the scenario execution within this method.
27+
Step definition classes (i.e. classes with the SpecFlow `[Binding]` attribute) are automatically added to the service collection.
2528

26-
A typical dependency builder method probably looks like this:
29+
A typical dependency builder method looks like this:
2730

2831
```csharp
2932
[ScenarioDependencies]
3033
public static IServiceCollection CreateServices()
3134
{
3235
var services = new ServiceCollection();
3336

37+
// TODO: add your test dependencies here
38+
// NOTE: since v0.4.0 it's no longer necessary to manually add your [Binding] classes
39+
3440
return services;
3541
}
3642
```
3743

38-
Refer to ```SpecFlow.DependencyInjection.Tests``` for a typical implementation.
44+
Refer to `SpecFlow.DependencyInjection.Tests` for an example.

SpecFlow.DependencyInjection.Tests/SpecFlow.DependencyInjection.Tests.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@
2323
<!-- .NET Core 3.1 LTS (always use latest applicable version) -->
2424
<ItemGroup Condition=" '$(TargetFramework)' == 'netcoreapp3.1' ">
2525
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="3.1.1" />
26-
<PackageReference Include="SpecFlow.Tools.MsBuild.Generation" Version="3.1.82" />
27-
<PackageReference Include="SpecFlow.xUnit" Version="3.1.82" />
26+
<PackageReference Include="SpecFlow.Tools.MsBuild.Generation" Version="3.1.86" />
27+
<PackageReference Include="SpecFlow.xUnit" Version="3.1.86" />
2828
</ItemGroup>
2929

3030
<ItemGroup>

SpecFlow.DependencyInjection/DependencyInjectionPlugin.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,6 @@ public void Initialize(RuntimePluginEvents runtimePluginEvents, RuntimePluginPar
4545
};
4646
}
4747

48-
/// <summary>
49-
/// Pass-through plumbing from Microsoft.Extensions.DependencyInjection to SpecFlow.BoDi.
50-
/// </summary>
5148
private void RegisterSpecFlowDependencies(
5249
IObjectContainer objectContainer,
5350
IServiceCollection services)
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using System;
2+
using TechTalk.SpecFlow;
3+
4+
namespace SolidToken.SpecFlow.DependencyInjection
5+
{
6+
[Serializable]
7+
public class MissingScenarioDependenciesException : SpecFlowException
8+
{
9+
public MissingScenarioDependenciesException()
10+
: base("No method marked with [ScenarioDependencies] attribute found.")
11+
{
12+
HelpLink = @"https://github.com/solidtoken/SpecFlow.DependencyInjection#usage";
13+
}
14+
}
15+
}

SpecFlow.DependencyInjection/ScenarioDependenciesAttribute.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ namespace SolidToken.SpecFlow.DependencyInjection
66
public class ScenarioDependenciesAttribute : Attribute
77
{
88
/// <summary>
9-
/// Automatically register all SpecFlow bindings
9+
/// Automatically register all SpecFlow bindings.
1010
/// </summary>
1111
public bool AutoRegisterBindings { get; set; } = true;
1212
}

SpecFlow.DependencyInjection/ServiceCollectionFinder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public Func<IServiceCollection> GetCreateScenarioServiceCollection()
2323
var services = createScenarioServiceCollection.Value;
2424
if (services == null)
2525
{
26-
throw new Exception("Unable to find scenario dependencies! Mark a static method that returns a IServiceCollection with [ScenarioDependencies]!");
26+
throw new MissingScenarioDependenciesException();
2727
}
2828
return services;
2929
}

0 commit comments

Comments
 (0)