ruby RSpec在钩子之前、之后和周围运行的顺序是什么?

wwwo4jvm  于 2023-08-04  发布在  Ruby
关注(0)|答案(3)|浏览(154)

当我面对some issue时,我决定检查beforeafter钩子的执行顺序。这就是我所做的:

  1. require "spec_helper"
  2. describe "The order:" do
  3. before(:all) {
  4. puts "before_all"
  5. }
  6. after(:all) {
  7. puts "after_all"
  8. }
  9. before(:each) {
  10. puts "before_each"
  11. }
  12. after(:each) {
  13. puts "after_each"
  14. }
  15. describe "DESC A" do
  16. before {
  17. puts "A_before"
  18. }
  19. it "A_it_1" do
  20. expect(1).to eq(1)
  21. end
  22. it "A_it_2" do
  23. expect(1).to eq(1)
  24. end
  25. end
  26. describe "DESC B" do
  27. before {
  28. puts "B_before"
  29. }
  30. it "B_it_1" do
  31. expect(1).to eq(1)
  32. end
  33. it "B_it_2" do
  34. expect(1).to eq(1)
  35. end
  36. end
  37. end

字符串
我得到了什么:

  1. The order:
  2. before_all
  3. DESC A
  4. before_each
  5. A_before
  6. after_each
  7. A_it_1
  8. before_each
  9. A_before
  10. after_each
  11. A_it_2
  12. DESC B
  13. before_each
  14. B_before
  15. after_each
  16. B_it_1
  17. before_each
  18. B_before
  19. after_each
  20. B_it_2
  21. after_all


这是怎么回事?为什么after_eachA_it_1之前运行?

更新:

添加around(:each)更有趣:

  1. around(:each) do |example|
  2. puts "around_in"
  3. example.run
  4. puts "around_out"
  5. end


结果:

  1. The order:
  2. before_all
  3. DESC A
  4. around_in
  5. before_each
  6. A_before
  7. after_each
  8. around_out
  9. A_it_1
  10. around_in
  11. before_each
  12. A_before
  13. after_each
  14. around_out
  15. A_it_2
  16. DESC B
  17. around_in
  18. before_each
  19. B_before
  20. after_each
  21. around_out
  22. B_it_1
  23. around_in
  24. before_each
  25. B_before
  26. after_each
  27. around_out
  28. B_it_2
  29. after_all

zfciruhq

zfciruhq1#

您的输出和www.example.com上记录的官方输出relishapp.com是正确的。实际上,rspec需要在每个示例之后运行after(:each),因为after(:each)中的异常会导致示例失败。在rspec可以在输出中显示示例之前,它需要知道它是绿色还是红色,这意味着在示例的描述出现在输出中之前,需要运行after(:eaches)。
然而,如果你在实际的例子中放入一个puts语句,你会看到before(:each)出现在它之前,然后运行例子代码(包括puts),然后是after(:each),就像你所期望的那样,最后,例子的描述被输出到屏幕上。
和你一样,我也很困惑,直到我意识到rspec打印出示例的标签与它实际做的事情不一致--只有为示例运行了所有before(:all)、before(:each)es和after(:each)es之后,标签才会被打印出来。
注意:after(:all)在示例标签被打印出来之后才运行,因为它们不会影响测试的结果(after(:all)钩子中发生异常时会生成一个警告,但这不会使测试变红)。

yvt65v4c

yvt65v4c2#

RSpec's documentation for before and after hooks指定它们运行的顺序。但是,RSpec's documentation for around hooks没有指定它们运行的顺序。
本规范测试aroundbeforeafter:all:each以及示例的执行顺序。当我使用rspec(-core)2.14.8运行它时,它们按照您期望的顺序执行:

  1. describe "order in which rspec around/before/after hooks run" do
  2. before :all do
  3. defined?($previous_hook).should be_false # this hook runs first
  4. $previous_hook = "before :all"
  5. end
  6. around :each do |example|
  7. $previous_hook.should == "before :all"
  8. $previous_hook = "around :each 1"
  9. example.run
  10. $previous_hook.should == "after :each"
  11. $previous_hook = "around :each 2"
  12. end
  13. before :each do
  14. $previous_hook.should == "around :each 1"
  15. $previous_hook = "before :each"
  16. end
  17. it "should not raise an exception or print anything" do
  18. $previous_hook.should == "before :each"
  19. $previous_hook = "example"
  20. end
  21. after :each do
  22. $previous_hook.should == "example"
  23. $previous_hook = "after :each"
  24. end
  25. after :all do
  26. # rspec ignores assertion failures and any other exceptions raised here, so all we can do is puts.
  27. # $previous_hook is a global because if it's an instance variable it is "before :all" at this point.
  28. warn "Previous hook was #{$previous_hook}, NOT around :each 2 as expected" unless $previous_hook == "around :each 2"
  29. end
  30. end

字符串
注意一些可能令人惊讶的事情:

  • self:all:each块中是不同的,所以我需要使用全局变量而不是示例变量。
  • after :all(但不是before :all)会吃异常。
  • 看看所有这些地方.should工作!你通常不会想在那里使用它。
展开查看全部
h7wcgrx3

h7wcgrx33#

这一点上面已经回答过了,只是再补充一个简单的回答方式。
要查看钩子运行的顺序,还必须在“it”中添加“puts”语句
所以呢

  1. describe "The order:" do
  2. before(:all) {
  3. puts "before_all"
  4. }
  5. after(:all) {
  6. puts "after_all"
  7. }
  8. before(:each) {
  9. puts "before_each"
  10. }
  11. after(:each) {
  12. puts "after_each"
  13. }
  14. describe "DESC A" do
  15. before {
  16. puts "A_before"
  17. }
  18. it "A_it_1" do
  19. # expect(1).to eq(1) <<<<---- Change Here
  20. puts "Inside the test"
  21. end
  22. end
  23. end

字符串

展开查看全部

相关问题