Using Test::Deep to test list of objects

What would you do if you wanted to test (using Test::More) whether a certain method call returns a list of objects?

Maybe use a while loop and test each element with an isa_ok($_ , ‘Object’)

But that would just increase the number of tests and if the method call returns an unknown number of objects then you won’t know in advance how many tests are to be run.

Another option is to cycle through the list and check each element using a if (ref($_) eq “Object”) and use a count variable to keep track of the number of objects. At the end of the loop, you can check the count variable against the number of elements in the list and accordingly set up a ok() test.

Too messy for me.

And that’s where the Test::Deep CPAN module can really help you out

Check this sample code:

use Test::More tests => 1;
use Test::Deep;

my @items = $feed->get_items;
cmp_deeply(\@items, array_each(isa(“Item”)), “get_items”);

Great!

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>