Testing fun

rob_au on 2003-03-14T13:32:31

In writing tests for some production code that I am working on, I have found myself wanting to independently test 'internal' object methods without using the object constructor. The reason for this is because of some of the sorting and queuing methods used within the object are quite extensive with much internal black magic, warranting their own test suite.

In order to solve this problem, I incorporated the following into a test script:

{
    #   The code braces here are designed to limit 
    #   variable scope for some nasty, internal object
    #   method testing - Overt your eyes children before
    #   it takes on its devil form!

    my $obj = bless [ undef, 0, [] ], 'My::Class';

    .
    .
}

Where the blessed array reference is the same as that blessed in the class constructor. This alternate constructor subsequently allowed the desired testing of internal methods using the $obj object.


Mock Objects

Dom2 on 2003-03-14T14:07:04

Would you have been better off using Test::MockObject?

See also MockObject.

-Dom

Re:Mock Objects

pdcawley on 2003-03-14T16:37:24

ObjectMother is another good testing pattern to look out for while you're about it. Sample code is in Java, but the idea is good.

Re:Mock Objects

rob_au on 2003-03-15T05:21:02

Thanks for the reply. I have looked at Test::MockObject previously and revisited it after reading your post - However, it seems that the real advantage of Test::MockObject is fulfilling module dependencies in testing and providing controlled return values from such modules.

This unfortunately was not the aim of my testing - With the code that I am testing, the standard class constructor builds a very complex queue of child objects from an XML configuration - By default, the enqueue and dequeue methods are private, only called from within the class. The testing which I was looking to perform with my class was the queuing and return methods, independent of the XML configuration and child objects. While I could use a [http://search.cpan.org/author/CHROMATIC/Test-MockObject/|Test::MockObject] object as the basis of testing, I believe that I would still need to bless this as an object of my class in order to effectively test these internal queuing and return methods. I might post something on this over on PerlMonks and see what alternate methods others also suggest.