RSpec - 匹配器

RSpec - 匹配器 首页 / RSpec入门教程 / RSpec - 匹配器

如果您还记得无涯教程最初的Hello World示例,则其中包含以下内容:

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

关键字eq是 RSpec 匹配器。在这里,将介绍RSpec中的其他类型的匹配器。

匹配器

匹配器以测试对象或值是否相等。

无涯教程网

MatcherRemarkExample
eqPasses when actual == expectedexpect(actual).to eq expected
eqlPasses when actual.eql?(expected)expect(actual).to eql expected
bePasses when actual.equal?(expected)expect(actual).to be expected
equalAlso passes when 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

执行以上代码后,将产生以下输出。秒数在您的计算机上可能会略有不同-

链接:https://www.learnfk.comhttps://www.learnfk.com/rspec/rspec-matchers.html

来源:LearnFk无涯教程网

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

比较匹配

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

MatcherRemarkExample
>Passes when actual > expectedexpect(actual).to be > expected
>=Passes when actual >= expectedexpect(actual).to be >= expected
<Passes when actual < expectedexpect(actual).to be < expected
<=Passes when actual <= expectedexpect(actual).to be <= expected
be_between inclusivePasses when actual is <= min and >= maxexpect(actual).to be_between(min, max).inclusive
be_between exclusivePasses when actual is < min and > maxexpect(actual).to be_between(min, max).exclusive
matchPasses when actual matches a regular expressionexpect(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

执行以上代码后,将产生以下输出。秒数在您的计算机上可能会略有不同-

链接:https://www.learnfk.comhttps://www.learnfk.com/rspec/rspec-matchers.html

来源:LearnFk无涯教程网

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

类型匹配

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

MatcherRemarkExample
be_instance_ofPasses when actual is an instance of the expected class.expect(actual).to be_instance_of(Expected)
be_kind_ofPasses when actual is an instance of the expected class or any of its parent classes.expect(actual).to be_kind_of(Expected)
respond_toPasses when actual responds to the specified method.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

执行以上代码后,将产生以下输出。秒数在您的计算机上可能会略有不同-

链接:https://www.learnfk.comhttps://www.learnfk.com/rspec/rspec-matchers.html

来源:LearnFk无涯教程网

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

true/false/nil匹配

用于测试值是true,false还是nil的匹配器。

MatcherRemarkExample
be truePasses when actual == trueexpect(actual).to be true
be falsePasses when actual == falseexpect(actual).to be false
be_truthyPasses when actual is not false or nilexpect(actual).to be_truthy
be_falseyPasses when actual is false or nilexpect(actual).to be_falsey
be_nilPasses when actual is nilexpect(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

执行以上代码后,将产生以下输出。秒数在您的计算机上可能会略有不同-

链接:https://www.learnfk.comhttps://www.learnfk.com/rspec/rspec-matchers.html

来源:LearnFk无涯教程网

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

错误匹配

当代码块引发错误时,用于测试的匹配器。

MatcherRemarkExample
raise_error(ErrorClass)当块引发 ErrorClass 类型的错误时通过。expect {block}.to raise_error(ErrorClass)
raise_error("error message")当块引发错误并显示消息“错误消息”时通过。expect {block}.to raise_error(“error message”)
raise_error(ErrorClass, "error message")当块引发带有“错误消息”消息的 ErrorClass 类型的错误时通过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

执行以上代码后,将产生以下输出。秒数在您的计算机上可能会略有不同-

链接:https://www.learnfk.comhttps://www.learnfk.com/rspec/rspec-matchers.html

来源:LearnFk无涯教程网

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

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

技术教程推荐

邱岳的产品手记 -〔邱岳〕

Swift核心技术与实战 -〔张杰〕

小马哥讲Spring核心编程思想 -〔小马哥〕

检索技术核心20讲 -〔陈东〕

正则表达式入门课 -〔涂伟忠〕

全链路压测实战30讲 -〔高楼〕

运维监控系统实战笔记 -〔秦晓辉〕

程序员职业规划手册 -〔雪梅〕

工程师个人发展指南 -〔李云〕

好记忆不如烂笔头。留下您的足迹吧 :)