Perhaps I'm wanting too much, but I think I'm seeing that an object mocked by Test::MockObject doesn't fool UNIVERSAL::isa and UNIVERSAL::can. Here's some code giving the idea:
use strict; use warnings FATAL => 'all'; use Test::MockObject; use Test::More tests => 6; my $c = Test::MockObject->new; $c->set_isa('Foo'); $c->mock('id', sub {return -1;}); ok($c->can('id'), "c can id (via 'can' method)"); ok($c->isa('Foo'), "c isa Foo (via 'isa' method)"); isa_ok($c, 'Foo', 'c isa_ok Foo'); is($c->id, -1, 'mocked accessor'); # FAILS! ok(UNIVERSAL::isa($c, 'Foo'), 'c isa Foo (via UNVERSAL::isa)'); # FAILS! ok(UNIVERSAL::can($c, 'id'), 'c can id (via UNIVERSAL::can');
Is this because UNIVERSAL is the root of the object hierarchy, and thus can't be fooled?

If so, it would seem that methods which need to check the type of their arguments (asserting if the wrong param type sent, for example), should use  $self->isa rather than UNIVERSAL::isa to do so, as otherwise you can't use Mock Objects to test the methods.

(In contrast, this nice perl OO style guide suggests UNIVERSAL:: over the method call approach. Which I think isn't great advice if you'll be testing with Test::MockObject.)

This isn't a SOPW post per se, but I end with the following question: are the observations above correct, or did I misuse  UNIVERSAL?

Thanks!

water

Retitled by davido from 'Class::MockObject doesn't fool UNIVERSAL'.


In reply to Test::MockObject doesn't fool UNIVERSAL by water

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.