Skip to content

Commit

Permalink
Implemented Context injection so that contexts could be shared across…
Browse files Browse the repository at this point in the history
… multiple scenario implementations
  • Loading branch information
xerxesb committed Dec 3, 2009
1 parent d79e211 commit 0c0b268
Show file tree
Hide file tree
Showing 13 changed files with 455 additions and 1 deletion.
12 changes: 11 additions & 1 deletion Runtime/ScenarioContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,17 @@ public object GetBindingInstance(Type bindingType)
object value;
if (!bindingInstances.TryGetValue(bindingType, out value))
{
value = Activator.CreateInstance(bindingType);
var ctors = bindingType.GetConstructors();
if (bindingType.IsClass && ctors.Length == 0)
throw new MissingMethodException(String.Format("No public constructors found for type {0}", bindingType.FullName));

var parameters = new List<object>();
foreach (var param in ctors[0].GetParameters())
{
parameters.Add(GetBindingInstance(param.ParameterType));
}

value = Activator.CreateInstance(bindingType, parameters.ToArray());
bindingInstances.Add(bindingType, value);
}

Expand Down
7 changes: 7 additions & 0 deletions TechTalk.SpecFlow.sln
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TechTalk.SpecFlow.Generator
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TechTalk.SpecFlow.Tools", "Tools\TechTalk.SpecFlow.Tools.csproj", "{87BE7FE6-C3DE-4409-ABF6-FA5B60AF3DE1}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FeatureTests", "Tests\FeatureTests\FeatureTests.csproj", "{3FE793A8-E662-4026-B4EC-891324073235}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -81,6 +83,10 @@ Global
{87BE7FE6-C3DE-4409-ABF6-FA5B60AF3DE1}.Debug|Any CPU.Build.0 = Debug|Any CPU
{87BE7FE6-C3DE-4409-ABF6-FA5B60AF3DE1}.Release|Any CPU.ActiveCfg = Release|Any CPU
{87BE7FE6-C3DE-4409-ABF6-FA5B60AF3DE1}.Release|Any CPU.Build.0 = Release|Any CPU
{3FE793A8-E662-4026-B4EC-891324073235}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{3FE793A8-E662-4026-B4EC-891324073235}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3FE793A8-E662-4026-B4EC-891324073235}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3FE793A8-E662-4026-B4EC-891324073235}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -90,5 +96,6 @@ Global
{F6740296-282C-4A0F-941E-A8FD1B1DAC2D} = {DCE0C3C4-5BC6-4A30-86BE-3FEFF4677A01}
{70376361-0BE1-478D-8EEC-47BD1C768165} = {A10B5CD6-38EC-4D7E-9D1C-2EBA8017E437}
{F8FACCF0-5497-4C6B-861F-78D72FD9561B} = {A10B5CD6-38EC-4D7E-9D1C-2EBA8017E437}
{3FE793A8-E662-4026-B4EC-891324073235} = {A10B5CD6-38EC-4D7E-9D1C-2EBA8017E437}
EndGlobalSection
EndGlobal
26 changes: 26 additions & 0 deletions Tests/FeatureTests/ContextInjection/ContextInjection.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
Feature: Injecting context into step specifications
As a developer
I would like to have the system automatically inject an instance of any class as defined in the constructor of a step file
So that I don't have to rely on the global shared state and can define the contexts required for each scenario.

Scenario: Feature with no context
Given a feature which requires no context
Then everything is dandy

Scenario: Feature with a single context
Given a feature which requires a single context
Then the context is set

Scenario: Feature with multiple contexts
Given a feature which requires multiple contexts
Then the contexts are set

Scenario: Feature with recursive contexts
Given a feature which requires a recursive context
Then the context is set
And its sub-context is set

Scenario: Feature with a dependent context
Given a feature which requires a single context
Then the context is set
And the context was created by the feature with a single context scenario
109 changes: 109 additions & 0 deletions Tests/FeatureTests/ContextInjection/ContextInjection.feature.cs

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using TechTalk.SpecFlow;

namespace FeatureTests.ContextInjection
{
[Binding]
public class FeatureWithADependentContextSteps
{
private readonly SingleContext _context;

public FeatureWithADependentContextSteps(SingleContext context)
{
_context = context;
}

[Given("a feature which requires a dependent context")]
public void GivenAFeatureWhichRequiresADependentContext()
{
}

[Then("the context was created by the feature with a single context scenario")]
public void ThenTheContextWasCreatedByTheFeatureWithASingleContextScenario()
{
Assert.That(_context.WasCreatedBy, Is.EqualTo("Feature With A Single Context"));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using TechTalk.SpecFlow;

namespace FeatureTests.ContextInjection
{
[Binding]
public class FeatureWithARecursiveContextSteps
{
private readonly NestedContext _context;

public FeatureWithARecursiveContextSteps(NestedContext context)
{
_context = context;
}

[Given("a feature which requires a recursive context")]
public void GivenAFeatureWhichRequiresARecursiveContext()
{
}

[Then("its sub-context is set")]
public void ThenItsSubContextIsSet()
{
Assert.That(_context.TheNestedContext, Is.Not.Null);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using TechTalk.SpecFlow;

namespace FeatureTests.ContextInjection
{
[Binding]
public class FeatureWithASingleContextSteps
{
private readonly SingleContext _context;

public FeatureWithASingleContextSteps(SingleContext context)
{
_context = context;
_context.WasCreatedBy = "Feature With A Single Context";
}

[Given("a feature which requires a single context")]
public void GivenAFeatureWhichRequiresASingleContext()
{
}

[Then("the context is set")]
public void ThenTheContextIsSet()
{
Assert.That(_context, Is.Not.Null);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using TechTalk.SpecFlow;

namespace FeatureTests.ContextInjection
{
[Binding]
public class FeatureWithMultipleContextsSteps
{
private readonly SingleContext _context1;
private readonly SingleContext _context2;

public FeatureWithMultipleContextsSteps(SingleContext context1, SingleContext context2)
{
_context1 = context1;
_context2 = context2;
}

[Given("a feature which requires multiple contexts")]
public void GivenAFeatureWhichRequiresMultipleContexts()
{
}

[Then("the contexts are set")]
public void ThenTheContextsAreSet()
{
Assert.That(_context1, Is.Not.Null);
Assert.That(_context2, Is.Not.Null);
}
}
}
22 changes: 22 additions & 0 deletions Tests/FeatureTests/ContextInjection/FeatureWithNoContextSteps.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TechTalk.SpecFlow;

namespace FeatureTests.ContextInjection
{
[Binding]
public class FeatureWithNoContextSteps
{
[Given("a feature which requires no context")]
public void GivenAFeatureWhichRequiresNoContext()
{
}

[Then("everything is dandy")]
public void ThenEverythingIsDandy()
{
}
}
}
17 changes: 17 additions & 0 deletions Tests/FeatureTests/ContextInjection/NestedContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
namespace FeatureTests.ContextInjection
{
public class NestedContext
{
private readonly SingleContext _context;

public NestedContext(SingleContext context)
{
_context = context;
}

public SingleContext TheNestedContext
{
get { return _context; }
}
}
}
7 changes: 7 additions & 0 deletions Tests/FeatureTests/ContextInjection/SingleContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace FeatureTests.ContextInjection
{
public class SingleContext
{
public string WasCreatedBy { get; set; }
}
}
Loading

0 comments on commit 0c0b268

Please sign in to comment.