Hello! Since I have what is probably and X/Y question, I'm going to describe both.

Context: I am developing unit tests for a Catalyst application. The Model is strictly relational, and consists of a straightforward Moose object many (100+) subroutines spread out over 15 or so Roles, but with associated SQL code somewhat specific to a database that I can't just make a little clone of for testing. I already have a system for testing this module that I am happy with. With that done, I would prefer to test the rest of the server code path with a fake DB object.

This would be roughly a "Mock" object, but I sometimes need it to be sensitive to input parameters, so call it "Mock+". It's technically easy to build such a thing, but given the large number of methods and the variety and sometimes complexity of the return values, I think it would take quite a bit of time.

I would prefer a record/replay solution like the following:

Record: One class takes another as a constructor argument, and then instantiates that object and "impersonates" its interface. When methods are called, it records the method and arguments as a "key", and return value as a "value". (Repeated keys can overwrite or discard a different return value -- it doesn't matter.) This goes to some file format either as the methods are called or at all at once when a method is called or the object is destructed.

Replay: Another class impersonates the same interface but doesn't instantiate the object. Instead, it turns the method/argument combinations into keys and returns the corresponding value if present. Otherwise it calls a default function (perhaps passing some version of the key, but I don't need that).

Return values need not include any function pointers, blessed objects and such. Parameter values of those types can just be treated as a string encoding the function or class name. Compromises such as turning the "key" into a unique string and storing an MD5 are fine.

So: 1) Does such a system already exist? And if not, 2) what projects/CPAN packages etc. would provide a useful reference or 3) what are the pieces with which such a (not necessarily CPAN-ready) system could be whipped up quickly?


In reply to Mock+ object with recording? by skef

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.