Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
RSpec:描述、上下文、功能、场景?_Rspec_Rspec2_Rspec Rails - Fatal编程技术网

RSpec:描述、上下文、功能、场景?

RSpec:描述、上下文、功能、场景?,rspec,rspec2,rspec-rails,Rspec,Rspec2,Rspec Rails,描述,上下文,功能,场景:四者之间的区别是什么,我什么时候使用它们?上下文是描述的别名,因此它们在功能上是等效的。您可以互换使用它们,唯一的区别是spec文件的读取方式。例如,测试输出没有区别。RSpec的书上说: “我们倾向于用descripe()表示事物,用context()表示上下文” 就我个人而言,我喜欢使用描述,但我能理解为什么人们更喜欢上下文 功能和场景是Capybara的一部分,而不是RSpec,用于验收测试功能相当于描述/上下文,场景相当于它/示例 如果您正在使用Capybara

描述
上下文
功能
场景
:四者之间的区别是什么,我什么时候使用它们?

上下文是
描述
的别名,因此它们在功能上是等效的。您可以互换使用它们,唯一的区别是spec文件的读取方式。例如,测试输出没有区别。RSpec的书上说:

“我们倾向于用
descripe()
表示事物,用
context()
表示上下文”

就我个人而言,我喜欢使用
描述
,但我能理解为什么人们更喜欢
上下文

功能
场景
是Capybara的一部分,而不是RSpec,用于验收测试<代码>功能相当于
描述
/
上下文
场景
相当于
/
示例


如果您正在使用Capybara编写验收测试,请使用
功能
/
场景
语法,如果不使用
描述
/
语法。

今天早上,在编写一些规范时,我遇到了同样的问题。通常,我主要使用
description
,并没有特别考虑这一点,但今天早上我处理了很多案例和一个模块的不同规格,因此下一个阅读这些规格的开发人员必须很容易理解。所以我问了谷歌这个问题,我发现这个:,我觉得他的回答很有趣:

根据rspec源代码,“context”只是“descripe”的别名方法,这意味着这两种方法之间没有功能上的区别。然而,使用这两种语言会有助于让你的测试更容易理解

“描述”的目的是针对一个功能包装一组测试,“上下文”是针对同一状态下的一个功能包装一组测试

因此,根据这一原则,您可以编写如下规范:

#
# 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
#
# The feature/behaviour I'm currently testing
#
describe "item ordering" do

  # 1st state of the feature/behaviour I'm testing
  context "without an order param" do
    # 1st and only test we want to run in this state
    it "asks the user for missing order param" do
     ...
    end
  end

  # 2nd state of the feature/behaviour I'm testing
  context "with an invalid order param" do
    # 1st test we want to run in this state
    it "validates and rejects order param" do
      ...
    end
    # 2nd test we want to run in this state
    it "returns an error to user" do
      ...
    end
  end

  # 3rd state of the feature/behaviour I'm testing with multiple tests
  context "with a valid order param" do
    it "validates and accepts order param" do
      ...
    end
    it "displays correct price for order" do
      ...
    end
    unless being_audited
      it "secretly charges higher price to user" do
        ...
      end
    end
  end
end
不确定这是否是一条普遍接受的规则,但我发现这种方法清晰易懂。

根据以下内容扩展Pierre的方法:

功能和场景DSL对应于描述和it, 分别地这些方法只是允许使用特征的别名 作为客户和验收测试,请阅读更多规范

因此,对于那些熟悉Mocha术语descripe和it(更适合从用户的角度描述测试行为,因此Mocha主要起到前端测试框架的作用)的人,您可以:

  • 选择始终且仅使用
    描述
    或其他配对
  • 选择在需要在特定应用程序状态下进行多个断言/测试的
    上下文
    块中使用
    it
使用第二个选项,您仍然可以遵循“在相同状态下针对一个功能包装[ping]一组测试”的意图

因此,您的测试可能如下所示:

#
# 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
#
# The feature/behaviour I'm currently testing
#
describe "item ordering" do

  # 1st state of the feature/behaviour I'm testing
  context "without an order param" do
    # 1st and only test we want to run in this state
    it "asks the user for missing order param" do
     ...
    end
  end

  # 2nd state of the feature/behaviour I'm testing
  context "with an invalid order param" do
    # 1st test we want to run in this state
    it "validates and rejects order param" do
      ...
    end
    # 2nd test we want to run in this state
    it "returns an error to user" do
      ...
    end
  end

  # 3rd state of the feature/behaviour I'm testing with multiple tests
  context "with a valid order param" do
    it "validates and accepts order param" do
      ...
    end
    it "displays correct price for order" do
      ...
    end
    unless being_audited
      it "secretly charges higher price to user" do
        ...
      end
    end
  end
end
通过这种方式,您可以完全跳过
功能
关键字,您可能希望将其用于特定的前端功能,或者如果您正在进行FDD(功能驱动开发),这可能会让一些人感到不舒服。在这里向您的开发团队征求意见


警告:不要总是遵循行业标准,想象一下如果我们按照大众汽车的理念对所有测试进行建模?

这是一个非常好的描述/上下文的答案。但是你忘记了关于功能/场景的问题的另一半-但是我认为它们可以(也应该)以与你在描述/上下文中指出的完全相同的方式使用。如果你通过对功能/场景的解释来扩展它,那就太好了!Sam说到点子上了,这里有一个链接,提供了更多细节和优秀的示例,特别是关于Capybara内置DSL(领域特定语言)的部分:似乎rubocop默认不会接受它