我想测试一个方法是用一个特定的参数递归调用的.
我的方法:
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
知道我的方法有什么问题吗?如何使用特定参数测试有效递归?
解决方法
@H_403_15@ 您现在的测试问题是您正在试验您要测试的方法. r.should_receive(:rec)正在用一个存根替换r#rec,这当然不会调用r.rec(7).更好的方法是简单地测试初始方法调用的结果是否正确.只要它最终给出了正确的答案,该方法是否可以递归,迭代或打电话给朋友,这一点并不严格.