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

Ruby:用MiniTest模拟一个类的方法?

发布时间:2020-12-16 19:59:03 所属栏目:百科 来源:网络整理
导读:我正在使用 MiniTest 2.12.1(Ruby的最新版本的测试框架 = 1.9),我不知道如何模拟一个类的方法,与 Mocha,例: product = Product.newProduct.expects(:find).with(1).returns(product)assert_equal product,Product.find(1) 我一直在互联网上玩了好几天,我还
我正在使用 MiniTest 2.12.1(Ruby的最新版本的测试框架> = 1.9),我不知道如何模拟一个类的方法,与 Mocha,例:
product = Product.new
Product.expects(:find).with(1).returns(product)
assert_equal product,Product.find(1)

我一直在互联网上玩了好几天,我还是找到一个合理的答案.请帮忙?

解决方法

如果你使用2.12.1,但是看起来他们在HEAD here中添加了方法stubbing to minitest / mock,这可能对你没有帮助.

那么,你是否更新到最小的HEAD,我想你可以这样做:

product = Product.new
Product.stub(:find,product) do
  assert_equal product,Product.find(1)
end

(编辑:李大同)

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

    推荐文章
      热点阅读