Rspec中的命令性和声明性步骤 [英] Imperative and declarative steps in Rspec

查看:82
本文介绍了Rspec中的命令性和声明性步骤的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道Rspec中的命令性步骤与声明性步骤到底是什么有关.

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

这是Rspec书中的示例代码:

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

我不太了解图片.

我了解的是,只要结果通过,声明式就可以让您做任何想要做的事情,而命令式则更加冗长.

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?

推荐答案

声明式是前进之路.

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

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.

这篇关于Rspec中的命令性和声明性步骤的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆