RSpec:描述,背景,特征,场景?

mai*_*aik 101 rspec rspec2 rspec-rails

describe,context,feature,scenario:什么是四间的差异(一个或多个),做当我使用每一个?

Sam*_*cey 136

context是别名describe,所以它们在功能上是等价的.您可以互换使用它们,唯一的区别是您的spec文件的读取方式.例如,测试输出没有区别.RSpec书中说:

"我们倾向于describe()用于事物和context()环境".

我个人喜欢使用describe,但我可以看到人们喜欢的原因context.

feature并且scenario是Capybara的一部分,而不是RSpec,并且用于验收测试.feature相当于describe/ context,scenario相当于it/ example.

如果您正在使用Capybara编写验收测试,请使用feature/ scenariosyntax,如果不是使用describe/ it语法.


Pie*_*son 33

今天早上,在写一些规格时,我遇到了同样的问题.通常,我主要使用describe并且没有特别考虑这个问题,但是今天早上我处理了一个模块的大量案例和不同的规范,所以对于下一个阅读这些规范的开发人员来说,它必须易于理解.所以我向谷歌询问了这一点,我发现了这个:在rspec中描述与上下文,我发现它的答案非常有趣:

根据rspec源代码,"context"只是"describe"的别名方法,这意味着这两种方法之间没有功能差异.但是,有一个上下文差异,通过使用它们有助于使您的测试更容易理解.

"describe"的目的是针对一个功能包装一组测试,而"context"是针对同一状态下的一个功能包装一组测试.

所以,依靠这个原则,你会写一个这样的规范:

#
# The feature/behaviour I'm currently testing
#
describe "item ordering" do

  # 1st state of the feature/behaviour I'm testing
  context "without a order param" do
    ...
  end

  # 2nd state of the feature/behaviour I'm testing
  context "with a given order column" do
    ..
  end

  # Last state of the feature/behaviour I'm testing
  context "with a given order column + reverse" do
    ..
  end
end
Run Code Online (Sandbox Code Playgroud)

不确定这是否是一个普遍接受的规则,但我发现这种方法很清楚,很容易掌握.