rspec 笔记

rspec的expect方法接收任何对象作为参数,并且返回一个rspec代理对象 叫做 ExpectationTarget。

ExpectationTarget存储了传递给expect方法的对象,他响应两个方法to和not_to (从技术上来说是三个, to_not是一个别名), to和not_to是普通的ruby方法,期望接收一个rspec matcher作为参数
rspe matcher没有什么特别的,仅仅是可以响应matchers?方法的对象, 你可以使用预定义的matchers或者自己定义

在我们的例子 expect(project.done?).to be_truthy 中, be_truthy是一个方法,rspec定义的,返回BeTruthy匹配器,和下面行为一样

expect(project.done?).to(RSpec::BuiltIn::BeTruthy.new)

ExpectationTarget现在存储两个对象 , 一个是被匹配的对象 project.done?和matchert叫做be_truthy。 当spec执行时, rspec在matcher调用 matches?使用使用这个Project.done?作为matcher的参数, 如果使用to, 这个maches?接收参数执行后,返回true就是通过测试, 如果使用not_to,检查matcher中有没有does_not_match?方法, 如果没有这个方法,就回头看maches?返回false表示测试通过

形式如be_whatever或者be_a_whatever关联了一个whatever?方法, 有一个问号标记在真实对象上,在我们前面的例子,expect(project.done?).to be_truthy 因为done?是一个断言方法,我们可以将这个例子写成, expect(project).to be_done

If the matcher is called via to , it passes if the predicate method returns a true value. If the matcher is called via not_to , it passes if the predicate method returns a false value.

if the predicate method starts with has , RSpec allows your matcher to start with have for readability (so your tests don’t look like they’ve been written by LOLCats); RSpec allows expect(actual).to have_key(:id) rather than expect(actual).to has_key(:id) .

RSpec 3 also allows you to chain multiple matchers using and and or , as in expect(actual).to include("a").and match(/.*3.*/) , or expect(actual).to eq(3).or eq(5) .

转载于:https://www.cnblogs.com/or2-/p/8450095.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值