Hi --

After an earlier question (Mocking Class::DBI stringification) about Test::MockObject, I pushed on a bit and then encountered (to me) some extremely befuddling behavior.

In the code below, after I reset a variable, $id='apple', the mocking breaks.

Why?

Thanks --

water

use strict; use warnings FATAL => 'all'; use Test::MockObject; use Test::More tests => 7; use Data::Dumper; use UNIVERSAL qw (isa can); package main; my $id = -1; sub return_id {return $id;} package MockFoo; # ! THIS IS THE *WRONG* WAY TO STRINGIFY A T:MO ?? use base qw(Test::MockObject); use overload '""' => \&::return_id; package main; my $c = MockFoo->new; $c->set_isa('Foo'); $c->mock('id', \&::return_id); is($id, -1, 'the bound scalar'); isa_ok($c, 'Foo'); ok ($c->isa('Foo'), 'and c isa foo, maybe'); ok(!isa($c,'Foo'), 'THIS SHOULD WORK (IMHO) but DOESNT, T:MO DOESNT FO +OL UNIVERSAL'); ok(!can($c, 'id'), 'THIS SHOULD WORK (IMHO) but DOESNT, T:MO DOESNT FO +OL UNIVERSAL'); is($c->id, -1, 'mocked accessor'); is($c->id, -1, 'mocked accessor repeated'); is('' . $c, -1, 'stringify'); $id='apple'; is($c->id, 'apple', 'apple: mocked accessor'); is('' . $c, 'apple', 'apple: stringify');

In reply to Test::MockObject subclassing befuddlement 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.