RSpec - 匹配器



如果你還記得我們最初的 Hello World 示例,它包含一行如下所示的程式碼 -

expect(message).to eq "Hello World!"

關鍵字 eql 是一個 **RSpec** “匹配器”。在這裡,我們將介紹 RSpec 中的其他型別的匹配器。

相等性/同一性匹配器

用於測試物件或值相等性的匹配器。

匹配器 描述 示例
eq 當 actual == expected 時透過 expect(actual).to eq expected
eql 當 actual.eql?(expected) 時透過 expect(actual).to eql expected
be 當 actual.equal?(expected) 時透過 expect(actual).to be expected
equal 當 actual.equal?(expected) 時也透過 expect(actual).to equal expected

示例

describe "An example of the equality Matchers" do 

   it "should show how the equality Matchers work" do 
      a = "test string" 
      b = a 
      
      # The following Expectations will all pass 
      expect(a).to eq "test string" 
      expect(a).to eql "test string" 
      expect(a).to be b 
      expect(a).to equal b 
   end
   
end

當以上程式碼執行時,將產生以下輸出。秒數在您的計算機上可能略有不同 -

.
Finished in 0.036 seconds (files took 0.11901 seconds to load)
1 example, 0 failures

比較匹配器

用於比較兩個值的匹配器。

匹配器 描述 示例
> 當 actual > expected 時透過 expect(actual).to be > expected
>= 當 actual >= expected 時透過 expect(actual).to be >= expected
< 當 actual < expected 時透過 expect(actual).to be < expected
<= 當 actual <= expected 時透過 expect(actual).to be <= expected
be_between 包含 當 actual <= min 且 >= max 時透過 expect(actual).to be_between(min, max).inclusive
be_between 排除 當 actual < min 且 > max 時透過 expect(actual).to be_between(min, max).exclusive
match 當 actual 匹配正則表示式時透過 expect(actual).to match(/regex/)

示例

describe "An example of the comparison Matchers" do

   it "should show how the comparison Matchers work" do
      a = 1
      b = 2
      c = 3		
      d = 'test string'
      
      # The following Expectations will all pass
      expect(b).to be > a
      expect(a).to be >= a 
      expect(a).to be < b 
      expect(b).to be <= b 
      expect(c).to be_between(1,3).inclusive 
      expect(b).to be_between(1,3).exclusive 
      expect(d).to match /TEST/i 
   end
   
end

當以上程式碼執行時,將產生以下輸出。秒數在您的計算機上可能略有不同 -

. 
Finished in 0.013 seconds (files took 0.11801 seconds to load) 
1 example, 0 failures

類/型別匹配器

用於測試物件型別或類的匹配器。

匹配器 描述 示例
be_instance_of 當 actual 是預期類的例項時透過。 expect(actual).to be_instance_of(Expected)
be_kind_of 當 actual 是預期類的例項或其任何父類的例項時透過。 expect(actual).to be_kind_of(Expected)
respond_to 當 actual 對指定方法做出響應時透過。 expect(actual).to respond_to(expected)

示例

describe "An example of the type/class Matchers" do
 
   it "should show how the type/class Matchers work" do
      x = 1 
      y = 3.14 
      z = 'test string' 
      
      # The following Expectations will all pass
      expect(x).to be_instance_of Fixnum 
      expect(y).to be_kind_of Numeric 
      expect(z).to respond_to(:length) 
   end
   
end

當以上程式碼執行時,將產生以下輸出。秒數在您的計算機上可能略有不同 -

. 
Finished in 0.002 seconds (files took 0.12201 seconds to load) 
1 example, 0 failures

真/假/空匹配器

用於測試值是否為真、假或空。

匹配器 描述 示例
be true 當 actual == true 時透過 expect(actual).to be true
be false 當 actual == false 時透過 expect(actual).to be false
be_truthy 當 actual 不為 false 或 nil 時透過 expect(actual).to be_truthy
be_falsey 當 actual 為 false 或 nil 時透過 expect(actual).to be_falsey
be_nil 當 actual 為 nil 時透過 expect(actual).to be_nil

示例

describe "An example of the true/false/nil Matchers" do
   it "should show how the true/false/nil Matchers work" do
      x = true 
      y = false 
      z = nil 
      a = "test string" 
      
      # The following Expectations will all pass
      expect(x).to be true 
      expect(y).to be false 
      expect(a).to be_truthy 
      expect(z).to be_falsey 
      expect(z).to be_nil 
   end 
end

當以上程式碼執行時,將產生以下輸出。秒數在您的計算機上可能略有不同 -

. 
Finished in 0.003 seconds (files took 0.12301 seconds to load) 
1 example, 0 failures

用於測試程式碼塊何時引發錯誤的匹配器。

錯誤匹配器

匹配器 描述 示例
raise_error(ErrorClass) 當代碼塊引發型別為 ErrorClass 的錯誤時透過。 expect {block}.to raise_error(ErrorClass)
raise_error("error message") 當代碼塊引發帶有訊息“error message”的錯誤時透過。 expect {block}.to raise_error(“error message”)
raise_error(ErrorClass, "error message") 當代碼塊引發型別為 ErrorClass 且訊息為“error message”的錯誤時透過 expect {block}.to raise_error(ErrorClass,“error message”)

示例

將以下程式碼儲存到名為 **error_matcher_spec.rb** 的檔案中,並使用以下命令執行它 - **rspec error_matcher_spec.rb**。

describe "An example of the error Matchers" do 
   it "should show how the error Matchers work" do 
      
      # The following Expectations will all pass 
      expect { 1/0 }.to raise_error(ZeroDivisionError)
      expect { 1/0 }.to raise_error("divided by 0") 
      expect { 1/0 }.to raise_error("divided by 0", ZeroDivisionError) 
   end 
end

當以上程式碼執行時,將產生以下輸出。秒數在您的計算機上可能略有不同 -

. 
Finished in 0.002 seconds (files took 0.12101 seconds to load) 
1 example, 0 failures
廣告

© . All rights reserved.