Imperative and declarative steps in Rspec

泄露秘密 提交于 2019-12-05 01:39:26

问题


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:


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.



来源:https://stackoverflow.com/questions/3466083/imperative-and-declarative-steps-in-rspec

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!