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

ruby – 如果使用any_instance,如何让stub返回调用它的实例?

发布时间:2020-12-17 02:02:59 所属栏目:百科 来源:网络整理
导读:例如,我有A班. class Aend 并希望在spec中从stubbed方法返回该类的实例. A.any_instance.stub(:my_method).and_return(here is the same instance on which my_method is called should got) 是否有可能在RSpec中做出类似的东西? 解决方法 这将为您解决问题
例如,我有A班.

class A
end

并希望在spec中从stubbed方法返回该类的实例.

A.any_instance.stub(:my_method).and_return(<here is the same instance on which my_method is called should got>)

是否有可能在RSpec中做出类似的东西?

解决方法

这将为您解决问题:

A.any_instance.stub(:my_method) do |*args|
    instance = RSpec::Mocks::space.send(:receivers).last
end

我在这里从rspec代码中挖出了这个:rspec github code

附: :这完全取决于rspec实现,并可能在将来发生变化.这里的背景有点复杂.我可以在将来添加它.

(编辑:李大同)

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

    推荐文章
      热点阅读