Rspec 个示例继续 运行 尽管遇到睡眠

Rspec examples continue to run in spite of sleep being encountered

我有以下rspec

context "example 1" do
  puts "hello"
  it "example 1 it" do
    sleep(50)
  end
end
context "example 2" do
  puts "thanks"
  it "example 2 it"
end
end

当我 运行 使用 rake 时,我得到以下输出:

hello
thanks

example 1
 example 1 it
  <<waits for 50s >>

为什么休眠50秒后打印thanks?我希望 thanks 仅在睡眠结束后打印。

puts 在 rspec 处理您的文件时执行,而不是与 运行 中的测试一起执行。为了向自己证明这一点,您可以尝试在 it 语句之后引用未定义的局部变量。

例如:

context "example 1" do
  puts "hello"
  it "example 1 it" do        
    sleep(50)
  end
  puts "goodbye" + cruel_world
end

当您 运行 rspec 时,您将看到您的 "hello",然后是

An error occurred while loading ./spec/your_spec.rb

接着是引用 cruel_world 的 NameError。