Skip to content

Commit

Permalink
Allow scenario events to be instance methods
Browse files Browse the repository at this point in the history
  • Loading branch information
gasparnagy committed Nov 10, 2009
1 parent 2ec81de commit 8341bd3
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 14 deletions.
20 changes: 16 additions & 4 deletions Runtime/BindingRegistry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,22 +81,34 @@ public List<EventBinding> GetEvents(BindingEvent bindingEvent)

private void BuildEventBindingFromMethod(MethodInfo method, BindingEventAttribute bindingEventAttr)
{
CheckEventBindingMethod(method);
CheckEventBindingMethod(bindingEventAttr.Event, method);

Delegate bindingAction = CreateBindingAction(method);
var eventBinding = new EventBinding(bindingAction, bindingEventAttr.Tags, method);

GetEvents(bindingEventAttr.Event).Add(eventBinding);
}

private void CheckEventBindingMethod(MethodInfo method)
private void CheckEventBindingMethod(BindingEvent bindingEvent, MethodInfo method)
{
if (!method.IsStatic)
throw new Exception("The event binding method must be static! " + method.ToString());
if (!IsScenarioSpecificEvent(bindingEvent) &&
!method.IsStatic)
throw errorProvider.GetNonStaticEventError(method);

//TODO: check parameters, etc.
}

private bool IsScenarioSpecificEvent(BindingEvent bindingEvent)
{
return
bindingEvent == BindingEvent.ScenarioStart ||
bindingEvent == BindingEvent.ScenarioEnd ||
bindingEvent == BindingEvent.BlockStart ||
bindingEvent == BindingEvent.BlockEnd ||
bindingEvent == BindingEvent.StepStart ||
bindingEvent == BindingEvent.StepEnd;
}

private void BuildStepBindingFromMethod(MethodInfo method, ScenarioStepAttribute scenarioStepAttr)
{
CheckStepBindingMethod(method);
Expand Down
7 changes: 7 additions & 0 deletions Runtime/ErrorHandling/ErrorProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,5 +81,12 @@ public Exception GetTooManyBindingParamError(int maxParam)
return new BindingException(
string.Format("Binding methods with more than {0} parameters are not supported", maxParam));
}

public Exception GetNonStaticEventError(MethodInfo methodInfo)
{
throw new BindingException(
string.Format("The binding methods for before/after feature and before/after test run events must be static! {0}",
GetMethodText(methodInfo)));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,47 +7,51 @@
namespace $rootnamespace$
{
[Binding]
public static class $safeitemname$
public class $safeitemname$
{
[BeforeStep]
public static void BeforeStep()
public void BeforeStep()
{
//TODO: implement logic that has to run before each scenario step
// For storing and retrieving scenario-specific or feature-specific data, the
// ScenarioContext.Current, FeatureContext.Current
// collections can be used.
// For storing and retrieving scenario-specific data,
// the instance fields of the class or the
// ScenarioContext.Current
// collection can be used.
// For storing and retrieving feature-specific data, the
// FeatureContext.Current
// collection can be used.
// Use the attribute overload to specify tags. If tags are specified, the event
// handler will be executed only if any of the tags are specified for the
// feature or the scenario.
// [BeforeStep("mytag")]
}

[AfterStep]
public static void AfterStep()
public void AfterStep()
{
//TODO: implement logic that has to run after each scenario step
}

[BeforeScenarioBlock]
public static void BeforeScenarioBlock()
public void BeforeScenarioBlock()
{
//TODO: implement logic that has to run before each scenario block (given-when-then)
}

[AfterScenarioBlock]
public static void AfterScenarioBlock()
public void AfterScenarioBlock()
{
//TODO: implement logic that has to run after each scenario block (given-when-then)
}

[BeforeScenario]
public static void BeforeScenario()
public void BeforeScenario()
{
//TODO: implement logic that has to run before executing each scenario
}

[AfterScenario]
public static void AfterScenario()
public void AfterScenario()
{
//TODO: implement logic that has to run after executing each scenario
}
Expand Down
1 change: 1 addition & 0 deletions changelog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ New features:
+ Finalize configuration (Issue 13)
+ Support German, French and Hungarian languages (Issue 5)
+ Add strong-name for specflow assemblies (Issue 2)
+ Allow scenario events to be instance methods (Issue 20)

Fixed issues:
+ Runtime: Remove direct dependency on nunit.framework.dll from the runtime (Issue 12)
Expand Down

0 comments on commit 8341bd3

Please sign in to comment.