加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – RSpec:匹配receive_message_chain的参数

发布时间:2020-12-17 03:18:46 所属栏目:百科 来源:网络整理
导读:我试图存根: Thing.where(uuid: options['uuid']).first 通过: allow(Thing).to receive_message_chain(:where,:first) .with(uuid: thing_double.uuid) .and_return(nil) 但这是回归: #Double (anonymous) received :first with unexpected arguments ex
我试图存根:

Thing.where(uuid: options['uuid']).first

通过:

allow(Thing).to receive_message_chain(:where,:first)
  .with(uuid: thing_double.uuid)
  .and_return(nil)

但这是回归:

#<Double (anonymous)> received :first with unexpected arguments
         expected: ({:uuid=>123})
              got: (no args)

我应该以不同的方式验证消息链的参数吗?

解决方法

当参数与最终方法不同时,您似乎不能与receive_message_chain结合使用.因此消息:

#<Double (anonymous)> received :first with unexpected arguments

这是有道理的 – RSpec如何知道链中的哪个方法应该接收参数?

要验证参数期望,不要存根链,只需存根

allow(Thing).to receive(:where).with(uuid: 1).and_return([])
expect(Thing.where(uuid: 1).first).to eq nil

或者省略参数:

allow(Thing).to receive_message_chain(:where,:first).and_return(nil)
 expect(Thing.where(uuid: 1).first).to eq nil

不建议使用receive_message_chain IMO.来自文档:

you should consider any use of receive_message_chain a code smell

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读