David,

> Also I think that my code's interface is nicer :-)

But we all think that about our interfaces, no? ;->

My personal interface for mocking looks more like this:

           class Mock::Property
           {
               has '_dbh' => ( is => 'ro', default => sub {
$Test::Rent::Dbh } );
               has 'businessmodel_tp' => ( is => 'ro', default => 'ppl' );

               method commit_changes () {}
           }

           Test::Rent::mock(
                   'Company::Property'     =>  'Mock::Property',
           );

And then the mock function just does this little bit of misdirection:

    my $mock = Test::MockObject->new;
    foreach my $class (keys %classes)
    {
        # this does several things:
        # 1) causes Perl to think the class we're replacing is already loaded
        # 2) creates a new() for the replaced class which returns an
instance of the substitute class
        # 3) reblesses the returned instance so code will think it
belongs to the replaced class
        # 4) uses an AUTOLOAD to catch all methods called on the
replaced class and redirects them
        #       to the substitute class
        no strict 'refs';
        use vars '$AUTOLOAD';
        $mock->fake_module($class,
                new         =>  sub { return bless
$classes{$class}->new, $class },
                AUTOLOAD    =>  sub { $AUTOLOAD =~ /::(\w+)\z/; my
$sub = "$classes{$class}::$1"; &$sub },
        );
    }

so that every time you _ask_ for a Company::Property, you actually
_get_ a Mock::Property.  And that's always seemed nice and intuitive
to me.  But I've always figured everyone else's intuitive was probably
far enough from mine that I've never tried to stick this out onto
CPAN.  (This method also has a number of disadvantages--you can't lie
to code about what class something is without it biting you sooner or
later--but it works surprisingly well, and there are very few problems
that I haven't been able to work around fairly easily.)

I dunno ... your interface is probably a bit nicer than
Test::MockObject's.  But I still like mine better. :-D


            -- Buddy

Reply via email to