在Rspec文件中,共享示例最后在示例列表中执行

时间:2013-09-09 04:29:03

标签: rspec rspec2

我遇到共享示例的执行问题。共享示例总是在示例列表的最后执行。如何在订单中运行所有示例?

我已经分享了看似

的示例规范

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

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

1 个答案:

答案 0 :(得分:1)

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

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

产生:

1
.3
.2
.

尽管有关不依赖于测试订单的评论,如果您希望describe块在同一级别it之前执行,我认为您需要放置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
.