Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In SpecFlow how can I share data between steps/features?

Tags:

c#

specflow

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?

like image 623
Simon Keep Avatar asked May 21 '10 11:05

Simon Keep


People also ask

How do I use scenario context in SpecFlow?

So how do you now access ScenarioContext? As of SpecFlow 3.0, you now need to use Context-Injection to acquire an instance of ScenarioContext by requesting it via the constructor. Once you have acquired the instance of ScenarioContext, you can use it with the same methods and properties as before.


1 Answers

In SpecFlow 1.3 there are three methods:

  1. static members
  2. ScenarioContext
  3. ContextInjection

Comments:

  1. static members are very pragmatic and in this case not so evil as we as developers might first think (there is no threading or need for mocking/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: https://docs.specflow.org/projects/specflow/en/latest/Bindings/Context-Injection.html

like image 51
jbandi Avatar answered Oct 02 '22 11:10

jbandi