RSpec - 匹配器


如果您还记得我们最初的 Hello World 示例,它包含一行,如下所示 -

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

关键字 eql 是一个RSpec “匹配器”。在这里,我们将介绍 RSpec 中其他类型的匹配器。

平等/身份匹配器

用于测试对象或值相等性的匹配器。

匹配器 描述 例子
情商 当实际 == 预期时通过 期望(实际).to eq 期望
情商 当实际.eql?(预期)时通过 预期(实际).to eql预期
是 当实际.等于?(预期)时通过 预期(实际)。预期
平等的 当实际.等于?(预期)时也通过 期望(实际).等于期望

例子

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

比较匹配器

用于与值进行比较的匹配器。

匹配器 描述 例子
> 当实际>预期时通过 期望(实际).to > 预期
>= 当实际 >= 预期时通过 期望(实际).to >= 预期
< 当实际 < 预期时通过 期望(实际).to < 预期
<= 当实际 <= 预期时通过 期望(实际).to <= 预期
包含在内 当实际值 <= min 且 >= max 时通过 期望(实际).to be_ Between(min, max).inclusive
be_ Between 排他性 当实际值 < 最小值且 > 最大值时通过 Expect(实际).to be_ Between(min, max).exclusive
匹配 当实际匹配正则表达式时通过 期望(实际).匹配(/正则表达式/)

例子

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

类/类型匹配器

用于测试对象类型或类的匹配器。

匹配器 描述 例子
成为实例 当实际是预期类的实例时通过。 期望(实际).to be_instance_of(预期)
是_kind_of 当实际是预期类或其任何父类的实例时通过。 期望(实际).to be_kind_of(预期)
回应 当实际响应指定方法时通过。 期望(实际)。响应(预期)

例子

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

True/False/Nil 匹配器

用于测试值是否为真、假或零的匹配器。

匹配器 描述 例子
是真实的 当实际 == true 时通过 期望(实际)为真
是假的 当实际 == false 时通过 期望(实际).为假
真实 当实际不为 false 或 nil 时通过 期望(实际).to be_truthy
是假的 当实际为 false 或 nil 时通过 期望(实际).to be_falsey
零 当实际为零时通过 期望(实际).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 类型的错误时通过。 期望 {block}.to raise_error(ErrorClass)
raise_error("错误信息") 当块引发错误并显示“错误消息”消息时通过。 期望 {block}.to raise_error(“错误消息”)
raise_error(ErrorClass, "错误信息") 当块引发 ErrorClass 类型的错误并显示消息“错误消息”时通过 期望 {block}.to raise_error(ErrorClass,“错误消息”)

例子

将以下代码保存到名为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