tags:

views:

24

answers:

1

I wonder what imperative vs declarative steps in Rspec is all about.

Here is an example code from the Rspec book:

Scenario: transfer money (declarative)
Given I have $100 in checking
And I have $20 in savings
When I transfer $15 from checking to savings
Then I should have $85 in checking
And I should have $35 in savings

Scenario: transfer money (imperative)
Given I have $100 in checking
And I have $20 in savings
When I go to the transfer form
And I select "Checking" from "Source Account"
And I select "Savings" from "Target Account"
And I fill in "Amount" with "15"
And I press "Execute Transfer"
Then I should see that I have $85 in checking
And I should see that I have $35 in savings

I don't quite get the picture.

What I have understood is that declarative let you do whatever you want as long as the result passes, and imperative is more verbose.

However, I don't feel that I have got the point of this.

Could someone explain this a little bit more. What are the differences and which one should I choose?

+1  A: 

Declarative is the way forward.

Imperative describes the actual UI steps you have to take as a user, rather than the results you're trying to achieve. If you write your scenarios this way they'll become really brittle and impossible to maintain. Imagine if someone put a confirmation box in that imperative scenario, and there were 80 similar scenarios which also required changing.

With declarative steps you only need to change it in the one place where the step is defined; that same declarative step is then reused for all the scenarios which need it.

Lunivore