oopplz has asked for the wisdom of the Perl Monks concerning the following question:

I recently had a discussion with a .NET programmer about extreme programming and different development paradigms. In a 'test first' paradigm we see the need for mock objects (objects that haven't been developed yet).
In .NET, my .NET programmer comments:

"Actually Mock Objects in the Unit Testing Context are a bit different than what you have here. A mock object is a class whose constructor takes in an instance of an object that implements an interface. Then you tell that object to ‘Expect’ calls to a particular method of that interface, with a given set of parameters or parameter types. You can also tell it what data you’d like it to return. Then what happens is when you execute the test, the mock object, keeps track of all the times that method (with those particular parameters) was called. If the method was called not enough, or too many times, then the test will fail, if it was called correctly, the correct number of times you specified, then the test passes."

He made these comments after I showed him how I make MOCK OBJECTS in perl:

use Class::Struct; struct Deck => { deck => '*@', slug => '*@', penetration => '$', # is value between 1->100 number_decks => '$', dealt => '*@', holeCard => '$', running_count => '$', dealCard => '$', enough_cards => '$' }; sub Deck::init { my ($self,%args) = @_; #$self->deck($args{deck} || 0); #$self->slug($args{slug} || 0); $self->penetration($args{penetration} || 0); $self->number_decks($args{number_decks} || 0); #$self->dealt($args{dealt} || 0); $self->holeCard($args{holeCard} || 0); $self->running_count($args{running_count} || 0); $self->dealCard($args{dealCard} || 0); $self->enough_cards($args{enough_cards} || 0); return $self; }

So you can imagine that while I am testing, I can create a mock object as a "stub" and use it normally (albeit the values will all be undef or 0 in the given case). After I verify that the object I am working works ok in the test section, I can then go and create the mock object "for real" in it's own seperate .pm file.

Obviously, the .NET MOCK OBJECT does more for the programmer than my way of using Class::Struct.

What I want to ask is this:

In perl, what is the "best" way of implementing mock objects? ("best" in qoutes because I am fully aware that there "is more than one way to do it!")

Replies are listed 'Best First'.
Re: testing with mock objects
by kaif (Friar) on Jun 10, 2005 at 00:18 UTC
Re: testing with mock objects
by eyepopslikeamosquito (Archbishop) on Jun 10, 2005 at 12:28 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?