Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

Using Sub::Override

by TStanley (Canon)
on Jun 18, 2007 at 23:06 UTC ( #621886=perlquestion: print w/replies, xml ) Need Help??
TStanley has asked for the wisdom of the Perl Monks concerning the following question:

After reading through the documentation for Test::MockObject, I decided that it was a slight case of overkill for what I wanted to do. After some research, I found Ovid's Sub::Override module, and decided to use that.
use Test::More tests=>1; use Games::QuizTaker; use Sub::Override; my $expected='Who is the creator of Perl?'; my $override=Sub::Override->new('Games::QuizTaker::IO::out'=>sub{ $Gam +es::QuizTaker::line }); my $GQ=Games::QuizTaker->new(filename=>'t/testqa'); $GQ->load; $GQ->generate; my $OUT=$GQ->test; like($OUT,qr/$expected/);

However, when I run 'make test', I get the following output:
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.......... Use of uninitialized value in pattern match (m//) at t/03test.t line 1 +6, <STDIN> line 1. # Failed test at t/03test.t line 16. # undef # doesn't match '(?-xism:Who is the creator of Perl?)' # Looks like you failed 1 test of 1. t/03test..........dubious + Test returned status 1 (wstat 256, 0x100) DIED. FAILED test 1 Failed 1/1 tests, 0.00% okay t/04exceptions....ok + t/05pod...........ok + Failed Test Stat Wstat Total Fail List of Failed ---------------------------------------------------------------------- +--------- t/03test.t 1 256 1 1 1 Failed 1/5 test scripts. 1/15 subtests failed. Files=5, Tests=15, 2 wallclock secs ( 0.46 cusr + 0.04 csys = 0.50 +CPU) Failed 1/5 test programs. 1/15 subtests failed. make: *** [test_dynamic] Error 255
It also hangs on the test file until I press the 'Enter' key
Here is the code for the test function that I am trying to test:
sub test{ my $self=shift; my $Answer_Sep=$self->get_AnswerDelimiter; my $Max=$self->get_MaxQuestions; my ($answer,$key,$line,$question_answer); my $question_number=1; my $number_correct=0; my $asep=qq"\\$Answer_Sep"; system(($^O eq "MSWin32"?'cls':'clear')); print"\n"; while($question_number<=$Max){ $key=shift @Randoms; print"Question Number $question_number\n"; foreach $line(@{$$questions{$key}}){ Games::QuizTaker::IO::out(wrap("","","$line\n")); } print"Your Answer: "; $answer=Games::QuizTaker::IO::in; chomp($answer); $answer=uc($answer);
Any pointers/hints as to what the simple thing that I am obviously overlooking would be greatly appreciated

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 Sub::Override
by chromatic (Archbishop) on Jun 19, 2007 at 00:58 UTC

    Do you mean:

    my $override=Sub::Override->new('Games::QuizTaker::IO::in'=>sub{ $Games::QuizTaker::line });
      No. The out function is what actually prints the question to STDOUT. The in function is what reads the answer. $line is a variable inside the test function of Games::QuizTaker. For each question in the test, it loops through an array, putting each element into the $line variable and prints it out.

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

        I don't have enough information to help you then. Your code has baffled me; I have no idea why you would override out() to return a variable when you call it in void context, and I can't see what you would get from not mocking in(), which presumably prompts for input. I never prompt for input in any of my automated tests.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://621886]
Approved by ferreira
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (4)
As of 2017-03-29 20:06 GMT
Find Nodes?
    Voting Booth?
    Should Pluto Get Its Planethood Back?

    Results (353 votes). Check out past polls.