Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/25.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
Ruby 如何在rspec示例之间设置一些依赖关系?_Ruby_Rspec_Dependencies - Fatal编程技术网

Ruby 如何在rspec示例之间设置一些依赖关系?

Ruby 如何在rspec示例之间设置一些依赖关系?,ruby,rspec,dependencies,Ruby,Rspec,Dependencies,我们需要的是运行一个示例,除非它的所有依赖项都已成功运行 即: 你能告诉我怎么做吗 多谢各位 问候,, Nouha < P>这说明了你需要重构你的规范——而不是依赖于在套件中较早运行的其他规范的成功或失败,考虑显式地配置要为每个规范测试的条件。这就是为什么RSPEC在方法之前提供的原因。目前,您并不是在真正测试应用程序代码,而是在测试测试套件的行为 在本例中,对于步骤4,将应用程序设置为测试1、2和3已成功,然后运行特定于步骤4的测试。最好尽可能地隔离测试中的代码,如果可能的话,不要在测试之间

我们需要的是运行一个示例,除非它的所有依赖项都已成功运行

即:

你能告诉我怎么做吗

多谢各位

问候,,
Nouha

< P>这说明了你需要重构你的规范——而不是依赖于在套件中较早运行的其他规范的成功或失败,考虑显式地配置要为每个规范测试的条件。这就是为什么RSPEC在方法之前提供<代码>的原因。目前,您并不是在真正测试应用程序代码,而是在测试测试套件的行为


在本例中,对于步骤4,将应用程序设置为测试1、2和3已成功,然后运行特定于步骤4的测试。最好尽可能地隔离测试中的代码,如果可能的话,不要在测试之间引入依赖关系。

这向我建议,您需要重新构建规范,而不是依赖于套件中早期运行的其他规范的成功或失败,考虑显式地配置要为每个规范测试的条件。这就是为什么RSPEC在方法之前提供<代码>的原因。目前,您并不是在真正测试应用程序代码,而是在测试测试套件的行为


在本例中,对于步骤4,将应用程序设置为测试1、2和3已成功,然后运行特定于步骤4的测试。最好尽可能地隔离测试中的代码,如果可能的话,不要在测试之间引入依赖关系。

正如@D_Bye所指出的,您可以使用before设置和验证示例组的前提条件,并且可以嵌套示例

describe "group example" do
  it "example 1" do
    ####
  end

  it "example 2" do
    ####
  end

  it "example 3" do
    ####
  end

  context "with preconditions" do
    before(:each) do
      # Establish the same preconditions as tested by examples
      # 1, 2, 3, or mark examples in this context as pending.
      begin
        ####
        raise "foo"
      rescue RuntimeError => e
        pending "preconditions not met: #{e.message}"
      end
    end

    it "example 4" do
      ####
    end
  end
end

您还可以只运行那些可以运行的示例(例如,取决于安装的软件)。

正如@D_Bye所指出的,您可以使用before设置和验证示例组的前提条件,并且可以嵌套示例

describe "group example" do
  it "example 1" do
    ####
  end

  it "example 2" do
    ####
  end

  it "example 3" do
    ####
  end

  context "with preconditions" do
    before(:each) do
      # Establish the same preconditions as tested by examples
      # 1, 2, 3, or mark examples in this context as pending.
      begin
        ####
        raise "foo"
      rescue RuntimeError => e
        pending "preconditions not met: #{e.message}"
      end
    end

    it "example 4" do
      ####
    end
  end
end

您也可以只运行那些可以运行的示例(例如,取决于安装的软件)。

规格/测试必须是独立的,切勿依赖订单,并相应地设计规格。规格/测试必须是独立的,永远不要依赖于订单,也不要据此设计规格。互联网上的第一个答案并不表明规格结构有问题。谢谢互联网上的第一个答案并不表明规范结构有问题。谢谢