How to test an iterator method

Hi guys,

I have a class that looks like this

class MyClass
def each_object
# do something really fun
end
end

I’d like to write some tests for the #each_object method but don’t
know how to. Could you please suggest a way to test methods that
iterate over the elements of a collection?

Thanks,
Edgardo

On 12/24/2009 01:14 AM, Edgardo H. wrote:

I’d like to write some tests for the #each_object method but don’t
know how to. Could you please suggest a way to test methods that
iterate over the elements of a collection?

Depends on what you want to check. If for example you want to make sure
elements are returned in the proper order you can do something like
this:

class X
include Test::Unit::Assertions

def test_a
v = 1…5
expect = [1,2,3,4,5]

 v.each do |x|
   assert_equal expect.shift, x
 end

end
end

This is of course just a silly test but should demonstrate the point.

Kind regards

robert