Hi,

I've got a class that calls methods like $self->method, which I'd like to write tests for. Because the internal method call might have state data I don't want to setup, I'd like to mock this method. For this, I thought I could use Test::MockObject::Extends.

Unfortunately, it looks like T::MO::E doesn't mock these methods. As an example, look at this class:
use strict; use warnings; package Foo; sub new { my $class = shift; return bless {}, $class; } sub bar { my $self = shift; $self->baz; return 1; } sub baz { die; } 1;
(This has to be in a separate file if you want to test this, because T::MO::E tries to 'use' the module when you call new()).

The test script below should be ok, but instead it dies (when bar() internally calls baz(), which I've attempted to mock):
use strict; use warnings; use Foo; use Test::MockObject::Extends; my $foo = Foo->new(); my $mock_foo = Test::MockObject::Extends->new( $foo ); $mock_foo->mock('baz',sub { 1 }); $mock_foo->bar;
Am I totally missing the point of Test::MockObject::Extends? Is my code untestable and in need of refactoring (probably, but not necessarily practical)? Any other suggestions?

Thanks.

In reply to Test::MockObject::Extends question by Mutant

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.