I'm currently working on a JBehave tutorial.

For my example I decided to implement the Addition feature from http://cukes.info/

Feature: Addition

In order to avoid silly mistakes

As a math idiot

I want to be told the sum of two numbers

Scenario: Add two numbers

Given I have entered 50 into the calculator

And I have entered 70 into the calculator

When I press add

Then the result should be 120 on the screen

Unfortunately, I have several issues with the above Scenario. For starters we're describing the behavior of Addition not that a Calculator can perform addition. The next issue I have is implicitly distinguishing numeric buttons from that of arithmetic ones. In my opinion both are actions. Typically whenever I press a button on my Calculator its state has changed somehow.

Here's my version of the above scenario:

Scenario: Add two numbers together

Given I want to do Addition

And my first operand is 50

And my second operand is 70

When I execute the Operation

Then I should get 120 as my result

Now let's compare and contrast each part. We'll start with how the state is specified.

Here's the cukes.info version:

Given I have entered 50 into the calculator

And I have entered 70 into the calculator

Ignoring the above issue about distinguishing numeric buttons from arithmetic ones, these two statements seem to imply that you've entered 5070 into the Calculator.

Note: I realize that if you read the Scenario as a whole it's evident that the author is using Reverse Polish Notation, but at this point we don't know that we're about to do addition. We only know that we've entered the numbers 50 and 70 into the Calculator.

Here's my version:

Given I want to do Addition

And my first operand is 50

And my second operand is 70

From this you can tell that I'm about to add the numbers 50 and 70 together.

Next we have how the action is described.

Here's the cukes.info version:

When I press add

In my opinion, this statement is breaking the open/closed principle. Suppose we wanted to add more operations like division and subtraction. In order to support them we'd have to open up the Calculator object and add new methods to it.

Here's my version:

When I execute the Operation

By using the Command Pattern and a simple hash map we can extend the Calculator's behavior without modifying any of its code.

Finally we have how the outcome is specified.

Here's the cukes.info version:

Then the result should be 120 on the screen

There's not much to say about this part other than what's a screen and how is it related to the operation of adding two numbers together. You might be able to argue that it's violating the Law of Demeter if screen is a label and the resulting code is calculator.getScreen( ).getText( ).

Here's my version:

Then I should get 120 as my result

## No comments:

## Post a Comment