diff --git a/Runtime/BindingRegistry.cs b/Runtime/BindingRegistry.cs index baf9ad283..2cd039929 100644 --- a/Runtime/BindingRegistry.cs +++ b/Runtime/BindingRegistry.cs @@ -81,7 +81,7 @@ public List 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); @@ -89,14 +89,26 @@ private void BuildEventBindingFromMethod(MethodInfo method, BindingEventAttribut 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); diff --git a/Runtime/ErrorHandling/ErrorProvider.cs b/Runtime/ErrorHandling/ErrorProvider.cs index 21a9439cd..be2707c49 100644 --- a/Runtime/ErrorHandling/ErrorProvider.cs +++ b/Runtime/ErrorHandling/ErrorProvider.cs @@ -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))); + } } } \ No newline at end of file diff --git a/VsIntegration/ItemTemplates/SpecFlowEventDefinition/SpecFlowEventDefinition1.cs b/VsIntegration/ItemTemplates/SpecFlowEventDefinition/SpecFlowEventDefinition1.cs index b8264c1f4..447854f00 100644 --- a/VsIntegration/ItemTemplates/SpecFlowEventDefinition/SpecFlowEventDefinition1.cs +++ b/VsIntegration/ItemTemplates/SpecFlowEventDefinition/SpecFlowEventDefinition1.cs @@ -7,15 +7,19 @@ 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. @@ -23,31 +27,31 @@ public static void BeforeStep() } [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 } diff --git a/changelog.txt b/changelog.txt index 02c958746..f6681d190 100644 --- a/changelog.txt +++ b/changelog.txt @@ -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)