Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: Writing a module test

by tirwhan (Abbot)
on Jun 09, 2007 at 13:20 UTC ( [id://620180]=note: print w/replies, xml ) Need Help??


in reply to Writing a module test

I'd probably refactor QuizTaker to add a Games::QuizTaker::IO module which implements the extremely simple methods in and out

sub in { return scalar <STDIN>; } sub out { print @_; }

You can then use Test::MockObject to mock G::QT:IO during your testing of the "test" method and pass data in/out as you want.

If you're not so much into mocking objects you could also fork the test and pass the data into your child from the test, or you could put the "test" call into a separate little script and run that via Test::Expect but IMO that's more convoluted. YMMV.


All dogma is stupid.

Replies are listed 'Best First'.
Re^2: Writing a module test
by TStanley (Canon) on Jun 09, 2007 at 21:00 UTC
    I decided the Test::MockObect route was the easier way to go (I couldn't get Test::Expect and its dependencies to complile on my Windows system, and jcwren's system already had T::MO installed)

    That said, I implemented the Games::QuizTaker:IO module and inserted it into the test section, so that it looks like the following:
    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);
    Well, the in method works fine, the out method has a slight problem. When I run a simple test script inside my development folder, this is what I get for output:
    Question Number 1 Games::QuizTaker::IOWhat popular operating system was created by a Fin +nish computer science student name Linus Torvalds? Games::QuizTaker::IOA. Unix Games::QuizTaker::IOB. Minux Games::QuizTaker::IOC. Linux Games::QuizTaker::IOD. Windows Your Answer:
    Any suggestions?


    UPDATE:
    I figured it out. I changed the call from Games::QuizTaker::IO->out to Games::QuizTaker::IO::out. That got rid of the module name in front of the answers. Now to get the test working :-)

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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://620180]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (4)
As of 2024-03-29 00:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found