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

ruby – Rspec:如何测试递归?

发布时间:2020-12-16 19:15:38 所属栏目:百科 来源:网络整理
导读:我想测试一个方法是用一个特定的参数递归调用的. 我的方法: class Recursable def rec(arg) rec(7) unless arg == 7 endenddescribe Recursable do it "should recurse" do r = Recursable.new('test') r.should_receive(:rec).with(0).ordered r.should_re
我想测试一个方法是用一个特定的参数递归调用的.

我的方法:

class Recursable
  def rec(arg)
    rec(7) unless arg == 7
  end
end

describe Recursable do
  it "should recurse" do
    r = Recursable.new('test')
    r.should_receive(:rec).with(0).ordered
    r.should_receive(:rec).with(7).ordered
    r.rec(0)
  end
end

出乎意料的是,RSpec失败了:

expected :rec with (7) once,but received it 0 times

知道我的方法有什么问题吗?如何使用特定参数测试有效递归?

解决方法

您现在的测试问题是您正在试验您要测试的方法. r.should_receive(:rec)正在用一个存根替换r#rec,这当然不会调用r.rec(7).

更好的方法是简单地测试初始方法调用的结果是否正确.只要它最终给出了正确的答案,该方法是否可以递归,迭代或打电话给朋友,这一点并不严格.

(编辑:李大同)

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

    推荐文章
      热点阅读