in reply to testing with mock objects

See also Stevan Little's articles at perl.com: Perl Code Kata: Testing Databases and Perl Code Kata: Mocking Objects.

Replies are listed 'Best First'.
Re^2: testing with mock objects
by oopplz (Novice) on Jun 13, 2005 at 04:56 UTC

    Thank you for the links kaif and eyepopslikeamosquito

    In a unit testing context, I could not figure out how to effectively use any of the pre-made Mock object modules available at CPAN. My own cooked up method using Class::Struct (see top node) seems to do the same thing.

    For instance, the following code doesn't see to do any more than what using Class::Struct would do:

    my $mock = Test::MockObject->new(); $mock->mock( 'fluorinate', sub { 'impurifying precious bodily fluids' } ); print $mock->fluorinate;

    Test::MockObject is at http://search.cpan.org/~chromatic/Test-MockObject-0.20/lib/Test/MockObject.pm

    I mean, if we compare the two approaches we see very little difference. Class::Struct automates the creation of accessor methods; so does Test::MockObject. They both create a blessed hash reference by default.

    They also have their differences. Adding subroutines (like init) is straight-forward using Class::Struct, whereas Test::MockObject would have the programmer creating wierdness with set_true, set_false, set_always, etc.

    To my eye, this doesn't look very clean.

    Test::MockObject does do something Class::Struct does not: it keeps track of any method calls to a Mock Object. I'm not sure why this is so useful anyway from the standpoint of unit testing.

    Can someone with more experience shed light on the subject?