http://www.perlmonks.org?node_id=620237

TStanley has asked for the wisdom of the Perl Monks concerning the following question:

After reading my copy of Perl Testing: A Developer's Notebook, and these two articles, I started to tackle the newest test file:
use Test::More tests=>4; use Test::MockObject; use strict; my $module='Games::QuizTaker'; my $TMO=Test::MockObject->new(); $TMO->fake_module($module => ('new' =>sub{ }, 'load' =>sub{ }, 'generate' =>sub{ }, 'test' =>sub{ })); use_ok($module); can_ok($module,'new'); my $gq=Games::QuizTaker->new(filename=>'t/testqa'); can_ok($module,'load'); $gq->load; can_ok($module,'generate'); $gq->generate;
However, make test is saying its a no go...
tstanley@perlmonk ~/modules/Games-QuizTaker $ make test PERL_DL_NONLAZY=1 /usr/bin/perl5.8.8 "-MExtUtils::Command::MM" "-e" "t +est_harness(0, 'blib/lib', 'blib/arch')" t/*.t t/01load..........ok + t/02generate......ok + t/03test..........ok 1/4Can't call method "load" on an undefined value + at t/03test.t line 18. # Looks like you planned 4 tests but only ran 3. # Looks like your test died just after 3. t/03test..........dubious + Test returned status 255 (wstat 65280, 0xff00) DIED. FAILED test 4 Failed 1/4 tests, 75.00% okay t/04exceptions....ok + t/05pod...........ok + Failed Test Stat Wstat Total Fail List of Failed ---------------------------------------------------------------------- +--------- t/03test.t 255 65280 4 2 4 Failed 1/5 test scripts. 1/18 subtests failed. Files=5, Tests=18, 1 wallclock secs ( 0.39 cusr + 0.04 csys = 0.43 +CPU) Failed 1/5 test programs. 1/18 subtests failed. make: *** [test_dynamic] Error 255 tstanley@perlmonk ~/modules/Games-QuizTaker $
As always, clues and/or hints are welcome

TStanley
--------
People sleep peaceably in their beds at night only because rough men stand ready to do violence on their behalf. -- George Orwell

Replies are listed 'Best First'.
Re: Using Test::MockObject
by chromatic (Archbishop) on Jun 10, 2007 at 06:39 UTC

    $gq is undef because your mocked constructor does nothing. You need to return $TMO or another mock object instead.

Re: Using Test::MockObject
by shigetsu (Hermit) on Jun 10, 2007 at 03:28 UTC

    Test::MockObject states (not so sure whether it's relevant to your case):

    If you use fake_module() to mock a module that already exists in memory -- one you've loaded elsewhere perhaps, but do not pass any subroutines to mock, this method will throw an exception. This is because if you call the constructor later on, you probably won't get a mock object back and you'll be confused.

    What happens if you try

    can_ok($module,'new'); can_ok($module,'load'); my $gq=Games::QuizTaker->new(filename=>'t/testqa'); $gq->load;
    instead?

      As long as the use_ok() call happens after the fake_module() call, this should make no difference.