tddbddspecflowgherkinacceptance-testing

How to use previously created objects in Gherkin?


I have a class named QuestionGroup which contains a list of Questions. supposing it has the following structure:

+QuestionGroup
  -Title

And a class named Question like this:

+Question
  -Title
  -Description
  -QuestionGroupId

And a class named InteractionGroup which contains a list of Employees:

+InteractionGroup
  -Title

and a class named Employee with:

+Employee
  -FirstName
  -LastName
  -InteractionGroupId

and I have a class named AppraisalTemplate . it contains a list of QuestionGroups and a list of InteractionGroups.

 +AppraisalTemplate
   -Title
   -List<QuestionGroup>
   -List<InteractionGroup>

now I want to write a specflow feature in which I need to create an AppraisalTemplate before my scenario runs.

my scenario depends on this AppraisalTemplate because in order to execute my When step in the scenario those steps should be processed already.

now this is the feature I have written (steps are not written yet):

Feature: Appraisals

Background:
    Given I have an QuestionGroup with Title '<QuestionGroupTitle>' and  following Questions
        | Title      | Description |
        | Question 1 | Desc Test 1 |
        | Question 2 | Desc Test 2 |

    And an InteractionGroup with Title '<InteractionGroupTitle>' and following employees
        | FirstName | LastName |
        | Clubber   | Lang     |
        | Mickey    | Goldmill |

    And an AppraisalTemplate with Title '<AppraisalTemplateTitle>' and following QuestionGroup and InteractionGroup

    #What should I write here?

Scenario Outline: [add a new appraisal]
    Given [given]
    When [when]
    Then [then]

I was wondering how should I write the 3rd Given step in Background section? I want to say

Given I have created an AppraisalTemplate with the **mentioned QuestionGroup(s) and InteractionGroup(s) in the last two steps**

How can I do it?

How can I execute a step and use a previously created objects in it?

Maybe I've misunderstood the whole story. If you would please explain me if there is a mistake in my feature as well.

thank you for your time


Solution

  • First of all you should try and simplify you scenario to

    1. Not use example groups
    2. Not use scenario outlines

    This will remove lots of complexity which distracts from your core question

    Then you should focus on the behaviour you are trying to exercise, which seems to be about filling in an Appraisal.

    Then write your Givens and Thens

    So ...

    When I fill in my appraisal
    

    To do this we might have something like

    Given my appraisal is due
    

    and we would implement this with something like

    Given "my appraisal is due" do
      @appraisal = create_appraisal(user: @I)
    end
    

    Now this requires me to be somebody, perhaps an employee

    Given I am an employee
    And my appraisal is due
    When I fill in my appraisal
    Then ...
    

    and we can implement that Given with

    Given "I am an employee" do
      @I = create_employee
    end
    

    So now we have left just the idea of creating an appraisal

    module AppraisalStepHelper
      def create_appraisal(user: )
        ...
      end
    end
    World AppraisalStepHelper
    

    Notice how there is nothing to do with HOW you interact with an appraisal in our feature or step definitions.

    So overall we have

    Feature: Employee appraisals
    
    ...
    
    Scenario: Fill in appraisal
      Given I am an employee
      And my appraisal is due
      When I fill in my appraisal
      Then ...
    

    Now you will probably need a load of over features to allow you to get to here. For example

    Feature: Schedule An Appraisal Feature: Create Appraisal Template ...

    There are several important points to note here

    You need to change your approach to Cuke effectively. Your current approach will overwhelm you with complexity. Hopefully the stuff above helps. Good luck