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 | |
by oopplz (Novice) on Jun 13, 2005 at 04:56 UTC |