如果您回想起我们原来的Hello World示例,它包含一行看起来像这样的&减去;
expect(message).to eq "Hello World!"
关键字eql是 RSpec "匹配器".在这里,我们将在RSpec中介绍其他类型的匹配器.
匹配器来测试对象或值的相等性.
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
匹配值的匹配器.
Matcher | 描述 | 示例 |
---|---|---|
eq | 实际= =预期时通过 | 期望(实际) .to eq expected |
eql | 当real.eql时传递?(预期) | 期望(实际).到预期的eql |
是 | 当actual.equal?(预期) | 期望时通过(实际).预期 |
等于 | 当actual.equal?(期望) | 期望(实际).等于预期 |
Matcher描述示例>实际时传递>预期期望(实际).是>预期> =实际时通过> ; =预期期望(实际).是> =预期<实际时通过<预期期望(实际).to<预期< =实际时通过< ; =预期期望(实际).to< =期望be_between inclusive当实际为< = min且> = maxexpect(actual).to be_between时传递(min,max).inclusivebe_between exclusive当实际值为< min和> maxexpect(actual).to be_between(min,max).exclusivematch当实际匹配正则表达式时传递期望(实际).匹配(/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(预期) |
be_kind_of | 当actual是预期类或其任何父类的实例时传递. | expect(actual).to be_kind_of(Expected) |
respond_to | 当实际响应指定的方法时通过. | expect(actual).to respond_to (预期) |
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 | 期望(实际).为真 |
是假 | 当实际通过==假 | 期望(实际).为假 |
be_truthy | 当实际不是假或零时通过 | 期望(实际) .to be_truthy |
be_falsey | 当real为false或nil时传递 | 期望(实际).to be_falsey |
be_nil | 当实际为零时通过 | 期望(实际).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
当代码块引发错误时,匹配器进行测试.
Matcher | 描述 | 示例 |
---|---|---|
raise_error(ErrorClass) | 当块引发ErrorClass类型的错误时传递. | expect {block} .to raise_error(ErrorClass) |
raise_error("error message") | 当块引发错误时传递消息"错误消息". | expect {block} .to raise_error("error message") |
raise_error(ErrorClass,"err或消息") | 当块引发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