I’d like to test that a method is called recursively with a specific argument.
My approach:
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
Unexpectedly, RSpec fails with:
expected :rec with (7) once, but received it 0 times
Any idea what’s wrong with my approach? How to test for effective recursion with a specific argument?
The problem with your test as it is now is that you are stubbing away the method you are trying to test.
r.should_receive(:rec)is replacingr#recwith a stub, which of course doesn’t ever callr.rec(7).A better approach would be to simply test that the result of the initial method call is correct. It shouldn’t strictly matter whether or not the method recurses, iterates, or phones a friend, as long as it gives the right answer in the end.