How to test an iterator method

E

Edgardo Hames

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
 
R

Robert Klemme

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?

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,077
Latest member
SangMoor21

Latest Threads

Top