Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Why mock doesn't work in this case?

by PerlOnTheWay (Scribe)
on Jan 06, 2012 at 09:45 UTC ( #946566=perlquestion: print w/ replies, xml ) Need Help??
PerlOnTheWay has asked for the wisdom of the Perl Monks concerning the following question:

try.t:
#! /usr/bin/perl use strict; use warnings; use Test::MockModule; my $father = Test::MockModule->new('Father'); $father->mock(alert => sub { print 'replaced by try' . $/; }); do 'Son.pm';
Father.pm
package Father; sub alert { print 'father' . $/; } 1
Son.pm:
package Son; use base Father; sub new { bless {}, Son; *{'Father::alert'} = sub { print 'son' . $/; }; } Son->new->alert();

When I run try.t,nothing is outputed

But if I delete the typeglob operation in Son.pm, I can get something outputed

why?

Comment on Why mock doesn't work in this case?
Select or Download Code
Re: Why mock doesn't work in this case?
by tobyink (Abbot) on Jan 06, 2012 at 10:05 UTC

    Son::new doesn't return the blessed object. Try:

    sub new { *{'Father::alert'} = sub { print 'son' . $/; }; bless {}, Son; }

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://946566]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (4)
As of 2014-07-13 05:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (247 votes), past polls