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

Commit

Permalink
Add test for Context Injection Scope (#13)
Browse files Browse the repository at this point in the history
Example for issue #12
  • Loading branch information
mbhoek committed Oct 30, 2019
1 parent 835eaee commit f6fcb08
Show file tree
Hide file tree
Showing 5 changed files with 244 additions and 0 deletions.
17 changes: 17 additions & 0 deletions SpecFlow.DependencyInjection.Tests/ContextInjectionScope.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
Feature: ContextInjectionScope
Issue #12: Scoping Context Injection to Scenario Execution Lifetimes
https://github.com/solidtoken/SpecFlow.DependencyInjection/issues/12

Scenario: Assert Context Is Scoped To Scenario Execution
The multiply and increase steps are intentionally implemented using different bindings.
Assert that they can operate on the same context (within the scenario executing).
Given I have test context with number 5
When I multiply the test context number by 2
And I increase the test context number by 3
Then the test context number should be 13

Scenario: Assert Context Is Scoped To Scenario Execution (No Spill)
Assert that the test context does not spill over to other scenarios.
Note that this assumes this scenario will be run after the above one (ie don't use parallel tests).
Given I have a test context
Then the test context number should be 0
140 changes: 140 additions & 0 deletions SpecFlow.DependencyInjection.Tests/ContextInjectionScope.feature.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

75 changes: 75 additions & 0 deletions SpecFlow.DependencyInjection.Tests/ContextInjectionScopeSteps.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
using System;
using System.Diagnostics;
using TechTalk.SpecFlow;
using Xunit;

namespace SolidToken.SpecFlow.DependencyInjection.Tests
{
public class TestContext
{
public int Number { get; set; }
}

[Binding]
public class ContextInjectionScopeSteps
{
private readonly TestContext _context;

public ContextInjectionScopeSteps(TestContext context)
{
_context = context;
}

[Given(@"I have a test context")]
public void GivenIHaveATestContext()
{
// NOOP
}

[Given(@"I have test context with number (.*)")]
public void GivenIHaveTestContextWithNumber(int number)
{
_context.Number = number;
}

[Then(@"the test context number should be (.*)")]
public void ThenTheTestContextNumberShouldBe(int expected)
{
Assert.Equal(expected, _context.Number);
}
}

[Binding]
public class MultiplySteps
{
private readonly TestContext _context;

public MultiplySteps(TestContext context)
{
_context = context;
}

[When(@"I multiply the test context number by (.*)")]
public void WhenIMultiplyTheTestContextNumberBy(int multiply)
{
_context.Number *= multiply;
}
}

[Binding]
public class IncreaseSteps
{
private readonly TestContext _context;

public IncreaseSteps(TestContext context)
{
_context = context;
}

[When(@"I increase the test context number by (.*)")]
public void WhenIIncreaseTheTestContextNumberBy(int increase)
{
_context.Number += increase;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@
</ItemGroup>

<ItemGroup>
<Compile Update="ContextInjectionScope.feature.cs">
<DesignTime>True</DesignTime>
<AutoGen>True</AutoGen>
<DependentUpon>ContextInjectionScope.feature</DependentUpon>
</Compile>
<Compile Update="DependencyInjectionPlugin.feature.cs">
<DesignTime>True</DesignTime>
<AutoGen>True</AutoGen>
Expand All @@ -47,6 +52,10 @@
</ItemGroup>

<ItemGroup>
<SpecFlowFeatureFiles Update="ContextInjectionScope.feature">
<Generator>SpecFlowSingleFileGenerator</Generator>
<LastGenOutput>ContextInjectionScope.feature.cs</LastGenOutput>
</SpecFlowFeatureFiles>
<SpecFlowFeatureFiles Update="DependencyInjectionPlugin.feature">
<Generator>SpecFlowSingleFileGenerator</Generator>
</SpecFlowFeatureFiles>
Expand Down
3 changes: 3 additions & 0 deletions SpecFlow.DependencyInjection.Tests/TestDependencies.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ public static IServiceCollection CreateServices()
// Add test dependencies
services.AddTransient<ITestService, TestService>();

// ContextInjectionScope (by using AddScoped instead of AddTransient, the context will be scoped to the Feature across bindings)
services.AddScoped<TestContext>();

// NOTE: This line is essential so that Microsoft.Extensions.DependencyInjection knows
// about the SpecFlow bindings (something normally BoDi does automatically).
// TODO: Find out if we can make this part of the Plugin
Expand Down

0 comments on commit f6fcb08

Please sign in to comment.