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

Commit f6fcb08

Browse files
authored
Add test for Context Injection Scope (#13)
Example for issue #12
1 parent 835eaee commit f6fcb08

File tree

5 files changed

+244
-0
lines changed

5 files changed

+244
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
Feature: ContextInjectionScope
2+
Issue #12: Scoping Context Injection to Scenario Execution Lifetimes
3+
https://github.com/solidtoken/SpecFlow.DependencyInjection/issues/12
4+
5+
Scenario: Assert Context Is Scoped To Scenario Execution
6+
The multiply and increase steps are intentionally implemented using different bindings.
7+
Assert that they can operate on the same context (within the scenario executing).
8+
Given I have test context with number 5
9+
When I multiply the test context number by 2
10+
And I increase the test context number by 3
11+
Then the test context number should be 13
12+
13+
Scenario: Assert Context Is Scoped To Scenario Execution (No Spill)
14+
Assert that the test context does not spill over to other scenarios.
15+
Note that this assumes this scenario will be run after the above one (ie don't use parallel tests).
16+
Given I have a test context
17+
Then the test context number should be 0

SpecFlow.DependencyInjection.Tests/ContextInjectionScope.feature.cs

Lines changed: 140 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
using System;
2+
using System.Diagnostics;
3+
using TechTalk.SpecFlow;
4+
using Xunit;
5+
6+
namespace SolidToken.SpecFlow.DependencyInjection.Tests
7+
{
8+
public class TestContext
9+
{
10+
public int Number { get; set; }
11+
}
12+
13+
[Binding]
14+
public class ContextInjectionScopeSteps
15+
{
16+
private readonly TestContext _context;
17+
18+
public ContextInjectionScopeSteps(TestContext context)
19+
{
20+
_context = context;
21+
}
22+
23+
[Given(@"I have a test context")]
24+
public void GivenIHaveATestContext()
25+
{
26+
// NOOP
27+
}
28+
29+
[Given(@"I have test context with number (.*)")]
30+
public void GivenIHaveTestContextWithNumber(int number)
31+
{
32+
_context.Number = number;
33+
}
34+
35+
[Then(@"the test context number should be (.*)")]
36+
public void ThenTheTestContextNumberShouldBe(int expected)
37+
{
38+
Assert.Equal(expected, _context.Number);
39+
}
40+
}
41+
42+
[Binding]
43+
public class MultiplySteps
44+
{
45+
private readonly TestContext _context;
46+
47+
public MultiplySteps(TestContext context)
48+
{
49+
_context = context;
50+
}
51+
52+
[When(@"I multiply the test context number by (.*)")]
53+
public void WhenIMultiplyTheTestContextNumberBy(int multiply)
54+
{
55+
_context.Number *= multiply;
56+
}
57+
}
58+
59+
[Binding]
60+
public class IncreaseSteps
61+
{
62+
private readonly TestContext _context;
63+
64+
public IncreaseSteps(TestContext context)
65+
{
66+
_context = context;
67+
}
68+
69+
[When(@"I increase the test context number by (.*)")]
70+
public void WhenIIncreaseTheTestContextNumberBy(int increase)
71+
{
72+
_context.Number += increase;
73+
}
74+
}
75+
}

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@
3333
</ItemGroup>
3434

3535
<ItemGroup>
36+
<Compile Update="ContextInjectionScope.feature.cs">
37+
<DesignTime>True</DesignTime>
38+
<AutoGen>True</AutoGen>
39+
<DependentUpon>ContextInjectionScope.feature</DependentUpon>
40+
</Compile>
3641
<Compile Update="DependencyInjectionPlugin.feature.cs">
3742
<DesignTime>True</DesignTime>
3843
<AutoGen>True</AutoGen>
@@ -47,6 +52,10 @@
4752
</ItemGroup>
4853

4954
<ItemGroup>
55+
<SpecFlowFeatureFiles Update="ContextInjectionScope.feature">
56+
<Generator>SpecFlowSingleFileGenerator</Generator>
57+
<LastGenOutput>ContextInjectionScope.feature.cs</LastGenOutput>
58+
</SpecFlowFeatureFiles>
5059
<SpecFlowFeatureFiles Update="DependencyInjectionPlugin.feature">
5160
<Generator>SpecFlowSingleFileGenerator</Generator>
5261
</SpecFlowFeatureFiles>

SpecFlow.DependencyInjection.Tests/TestDependencies.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ public static IServiceCollection CreateServices()
1515
// Add test dependencies
1616
services.AddTransient<ITestService, TestService>();
1717

18+
// ContextInjectionScope (by using AddScoped instead of AddTransient, the context will be scoped to the Feature across bindings)
19+
services.AddScoped<TestContext>();
20+
1821
// NOTE: This line is essential so that Microsoft.Extensions.DependencyInjection knows
1922
// about the SpecFlow bindings (something normally BoDi does automatically).
2023
// TODO: Find out if we can make this part of the Plugin

0 commit comments

Comments
 (0)