tags:

views:

288

answers:

3

I have 2 features that use a common 'When' step but have different 'Then' steps in different classes.

How do I access, for example, the ActionResult from my MVC controller call in the When step in my two Then steps?

+2  A: 

Use the ScenarioContext class which is a dictionary that is common to all the steps.

ScenarioContext.Current.Add("ActionResult", actionResult);

var actionResult = (ActionResult) ScenarioContext.Current["ActionResult"];
Si Keep
A: 

Currently I am just using a static class with static properties for all data that I want to share between steps. I am not sure how well that would work with MVC though since I have not used it with SpecFlow.

INTPnerd
+4  A: 

In SpecFlow 1.3 there are three methods:

  1. static members
  2. ScenarioContext
  3. ContextInjection

Comments:

  1. static members are very pragmatic adn in this case not so evil as we as developers might first think (there is no threading or need formocking/replacing in step-definitions)

  2. See answer from @Si Keep in this thread

  3. If the constructor of a step definition class needs arguments, Specflow tries to inject these arguments. This can be used to inject the same context into several step-definitions. See an example here: http://github.com/techtalk/SpecFlow/tree/master/Tests/FeatureTests/ContextInjection/

jbandi
i think instance variables can be used as well, as in one of their examples: http://github.com/techtalk/SpecFlow-Examples/blob/master/BowlingKata/BowlingKata-Nunit/Bowling.Specflow/BowlingSteps.cs
Carl Hörberg

related questions