RSpec - 標記



RSpec 標記提供了一種簡單的方法來執行規範檔案中的特定測試。預設情況下,RSpec 會執行它執行的所有規範檔案中全部測試,但你可能只需要執行其中一個子集。讓我們說你有一些執行非常快速的測試,並且你剛剛對應用程式程式碼做出了更改,只想執行快速測試,此程式碼將演示如何使用 RSpec 標記來實現這一點。

describe "How to run specific Examples with Tags" do 
   it 'is a slow test', :slow = > true do 
      sleep 10 
      puts 'This test is slow!' 
   end 
   
   it 'is a fast test', :fast = > true do 
      puts 'This test is fast!' 
   end 
end

現在,將上述程式碼儲存在名為 tag_spec.rb 的新檔案中。從命令列,執行以下命令:rspec --tag slow tag_spec.rb

你將看到以下輸出 −

執行選項:包含 {: slow = >true}

This test is slow! 
. 
Finished in 10 seconds (files took 0.11601 seconds to load) 
1 example, 0 failures

然後,執行以下命令:rspec --tag fast tag_spec.rb

你將看到以下輸出 −

Run options: include {:fast = >true} 
This test is fast! 
. 
Finished in 0.001 seconds (files took 0.11201 seconds to load) 
1 example, 0 failures

如你所見,RSpec 標記使執行測試子集變得非常容易!

廣告
© . All rights reserved.