我想知道Rspec中的命令性和声明性步骤是什么.
以下是Rspec书中的示例代码:
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
Run Code Online (Sandbox Code Playgroud)
我不太清楚.
我所理解的是,只要结果通过,声明式让你做任何你想做的事情,而命令式更加冗长.
但是,我觉得我没有明白这一点.
有人可以解释一下这一点.有什么区别,我应该选择哪一个?
声明是前进的方向.
命令描述了您作为用户必须采取的实际UI步骤,而不是您尝试实现的结果.如果以这种方式编写场景,它们将变得非常脆弱且无法维护.想象一下,如果有人在该命令性场景中放置了一个确认框,并且有80个类似的场景也需要更改.
使用声明性步骤,您只需在定义步骤的位置更改它; 然后,对于需要它的所有场景,重用相同的声明步骤.