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!")


In reply to testing with mock objects by oopplz

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.