DEV Community

Matheus Morett
Matheus Morett

Posted on

πŸ” "Overlooked Use Cases in Your Tasks?"

Throughout my 8️⃣ years in software development, I've faced a recurring challenge: poorly specified tasks. 😰

How many times were use cases overlooked by the PO? And what if, during development, I forgot a crucial detail? πŸ€” These oversights often only emerged during testing or, even worse, once users began interacting with the application. πŸ˜“

This scenario began to shift for the better when I was fortunate enough to work with a PO who employed Gherkin for specifications. 🌟 From that point on, the responsibility to define use cases was shared between the PO and myself. This change not only made it easier to introduce automated testing but also helped me deliver more bug-free software. πŸ› οΈπŸ’‘

Let's dive into a practical example. 🧐

Imagine you were given this User Story:

As a user, I should be able to redeem a discount coupon at checkout. πŸ›οΈ
Enter fullscreen mode Exit fullscreen mode

Translating this User Story to automated tests in Jest:

describe('Coupon Redemption based on User Story', () => {
  it('should allow users to redeem a discount coupon', () => {
    // Mock checkout process
    // Insert coupon code
    expect(finalPrice).toBeDiscounted();
  });
});
Enter fullscreen mode Exit fullscreen mode

However, this test seems quite general and lacks detail.

Now, let's see how Gherkin can refine this: πŸ‘‡

Scenario: Redeeming a valid coupon
Given the user is finalizing a purchase
When they enter a valid coupon code
Then the appropriate discount should be applied to the total 🟒

Scenario Redeeming an expired coupon
Given the user is finalizing a purchase
When they enter an expired coupon code
Then no discount should be applied
And a message stating the coupon has expired should be displayed. πŸ”΄
Enter fullscreen mode Exit fullscreen mode

Translating the refined Gherkin scenarios to Jest tests:

describe('Coupon Redemption based on Gherkin', () => {
  it('should apply discount for valid coupon', () => {
    // Mock checkout process
    // Input valid coupon code
    expect(finalPrice).toBeDiscounted();
  });

  it('should show error for expired coupon', () => {
    // Mock checkout process
    // Input expired coupon code
    expect(finalPrice).not.toBeDiscounted();
    expect(displayErrorMessage).toBeCalledWith('Coupon expired');
  });
});
Enter fullscreen mode Exit fullscreen mode

The Gherkin specifications not only address the scenario of a valid coupon but also consider the situation of an expired coupon, which could easily be overlooked with a vaguer User Story.

Top comments (0)