Rspec 中的命令式步骤和声明式步骤

发布于 2024-09-14 11:51:02 字数 793 浏览 3 评论 0原文

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

我不太明白。

我的理解是,声明式让你可以做任何你想做的事情,只要结果通过,而命令式则更冗长。

然而,我不觉得我已经明白了这一点。

有人可以解释一下吗?它们有什么区别,我应该选择哪一种?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

恏ㄋ傷疤忘ㄋ疼 2024-09-21 11:51:02

声明式是前进的方向。

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

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文