【问题标题】:Imperative and declarative steps in RspecRspec 中的命令式和声明式步骤
【发布时间】:2010-08-12 08:59:03
【问题描述】:

我想知道 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

我不太明白。

我的理解是,声明式让你只要结果通过就可以为所欲为,而命令式则更冗长。

但是,我觉得我没有明白这一点。

有人能解释一下吗?有什么区别,我应该选择哪一个?

【问题讨论】:

    标签: rspec bdd


    【解决方案1】:

    声明式是前进的方向。

    命令式描述了您作为用户必须采取的实际 UI 步骤,而不是您试图实现的结果。如果您以这种方式编写场景,它们将变得非常脆弱且无法维护。想象一下,如果有人在该命令式场景中放置一个确认框,并且有 80 个类似的场景也需要更改。

    使用声明性步骤,您只需在定义步骤的地方进行更改;然后将相同的声明性步骤重用于所有需要它的场景。

    【讨论】:

    • 耦合问题的好例子,但是当您使用验收测试来定义用户故事中的预期内容时,明确定义步骤很重要。声明性步骤隐藏了 en.wikipedia.org/wiki/Specification_by_example 所需的详细信息。 github.com/cheezy/page-object 看起来是在不牺牲所需细节的情况下解决 UI 耦合问题的一个很好的开始。
    • 我发现 BDD 在您可以捕获对话而不是精确指定 UI 交互时效果最好。这也是丹首先拿出“测试”这个词的原因之一。不过,同意使用页面对象范例。
    猜你喜欢
    • 1970-01-01
    • 2016-02-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-04
    • 2021-07-03
    • 1970-01-01
    相关资源
    最近更新 更多