1

I have issue with execution of shared examples.The shared example always gets executed at the end in the list of examples. How to run all the examples in the order ?

I have shared example spec which looks like

sharedExamples_spec.rb

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

And in other spec file

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 Output I get is

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

If you see the the output, Shared examples is executed as the last example. Can anyone please suggest how to execute the test in the order that is written.

4

1 回答 1

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
.
于 2013-09-09T15:10:31.907 回答