Search code examples
bddgherkinuser-stories

Given When then scenario for automatic email notification


How would you write a Given when then construct for a email notification scenario where a notification has to be sent 1 business day after a particular state change?

Does this look right?

Given that I'm the system 
  and the user of a particular type T
  and the state is S
When it is 1 business day after the date state was changed to x
then send an email notification with xxx content.

The reason I have a doubt is that my understanding is that Given section us for preconditions which in this cases is A. particular type of user, B. state is S and C. it is 1 day past a the date the state changed.

If all these preconditions are in Given section, what will be in the when section? My understanding was that 'when' section is for the event trigger. There's no action other than running the notification job. So would this be correct?

Given the user of a particular type T
  and the state is S
  and it is 1 business day after the date state was changed to x
When the email notification process triggered
  then send an email notification to all users of type T with xxx content.

I'd appreciate your thoughts


Solution

  • You are on the right track!

    So as you said before When is a trigger, so lets look into this deeper.

    Gherkin is used for Behavior Driven Development (BDD) in that you are trying to simulate behavior.

    Given - Input - Prerequisites

    These are things that need are expected before the action or trigger occurs. They set up your tests for the action that will occur.

    When - Trigger - Action

    These are the users behavior that triggers the result. This is the B in BDD

    Then - Output - Result

    This is the end result, what should the user expect when the behavior completes.

    Where You Stand

    So you are in an interesting situation because as you pointed out, there is no behavior. So heres the tricky part, while having a Given-When-Then format is nice, its totally valid to have a Given-Then or a When-Then format as well in cases where there is no solid user behavior. So in that case:

    Given it is 1 business day after the date state was changed to x
    

    and

    When it is 1 business day after the date state was changed to x
    

    Are both equally as valid.

    In regards to your second code block, the question you need to ask yourself is the email notification process triggered a trigger or a result? Personally I would say it is a result of the Inputs. If you do think you need it then I think you should write your test like this:

    Given there is a user of type T
    And the state is S
    And it is 1 business day after the date state was changed to x
    Then the email notification process should have triggered
    And send an email notification to all users of type T with xxx content.
    

    Bonus -- How to word your test and avoid implementation language

    Not that you asked, but there is actually a way to improve your tests wording. You want to decouple your test from the actual implementation. What this means is avoid words like Send, Click, or Type and focus on behavior and result. Taking your test I would reword it like this:

    Given there is a user of type T
    And the state is S
    And it is 1 business day after the date state was changed to x
    Then all users of type T should be notified of xxx
    

    I removed the email notification step because that can be inferred from the last step. I also decoupled the last step from implementation language. Whats important here isn't that the user gets sent an email, its that they are notified of the content. Your Given steps should also be changed in my opinion depending on your business logic as they are currently very coupled to your implementation.