在 Rspec 文件中,共享示例在示例列表的末尾执行



我在执行共享示例时遇到问题。共享示例始终在示例列表的末尾执行。如何按顺序运行所有示例?

我分享了示例规范,看起来像

sharedExamples_spec.rb

shared_examples "upload jar" do |msg|
  it "shared examples group" do
    sleep(10)
    p "hello there :2 #{msg}"
  end 
end

并在其他等级库中

require 'sharedExamples_spec.rb'
    describe "something" do
       before(:each) do
         @spec = "something group"
       end

 it "1: does something" do
   puts "hello there:1 #{@spec}"
 end
 describe "shared example" do
   it_should_behave_like "upload jar"," shared group"
end
it "3: does something" do
  puts "hello there:3 #{@spec}"
end
end

我得到的Rspec输出是

something
 hello there:1 something group
  1: does something
 hello there:3 something group
  3: does something
 shared example
   it should behave like upload jar
    "hello there :2  shared group"
  shared examples group
 Finished in 1 second
  3 examples, 0 failures

如果看到输出,则共享示例将作为最后一个示例执行。 任何人都可以建议如何按照编写的顺序执行测试。

我不

认为这种现象与共享示例本身有任何关系。在给定的describe块中,RSpec 似乎在运行任何嵌套describe示例之前运行所有it示例,如下所示:

describe "order test" do
  it {puts 1}
  describe "nested describe" do
      it {puts "2"}
    end
  it {puts 3}
end

它产生:

1
.3
.2
.

尽管有关于不依赖于测试顺序的评论,但如果您希望在同一级别的it之前执行describe块,我认为您需要将it放在它自己的describe块中。在您的情况下,如下所示:

describe "something" do
  before(:each) do
    @spec = "something group"
  end
  shared_examples "upload jar" do |msg|
    it "shared examples group" do
      p "hello there :2 #{msg}"
    end 
  end
  describe "example 1" do
    it "1: does something" do
      puts "hello there:1 #{@spec}"
    end
  end
  describe "shared example" do
    it_should_behave_like "upload jar"," shared group"
  end
  describe "example 3" do
    it "3: does something" do
      puts "hello there:3 #{@spec}"
    end
  end
end

它产生:

hello there:1 something group
."hello there :2  shared group"
.hello there:3 something group
.

最新更新