[prev UnitTestingMyLibraryGracefulExit | next UnitTestingMyLibraryPrepareAndExecuteSql | top UnitTestingMyLibrary] Next up is: sub returnError { my $errorMessage = @_[0]; &printHeader; # NCSU address hard coded here that is shown to the public # Perhaps a webmaster and address needs to be in the db print qq(
ERROR: $errorMessage
Email Eric.); &gracefulExit; } The new thing here is an explicit argument for the subroutine. This primarily means a change to the way we call the subroutine, which complicates the trap_stdout routine. sub trap_stdout { my($unit_ref, $args, $result, $io, $old_handle); $unit_ref = $_[0]; $args = $_[1]; print STDOUT @args; $io = IO::String->new($result); $old_handle = select($io); &$unit_ref(@$args); select($old_handle); return $result; } There are calls to printHeader and gracefulExit that need to be factored out, since we've already done separate UnitTest''''''s. To do this, all we need to pass is enough to keep errors from occurring. That means passing in a database handle and a CGI object. # ---------- tests for &M''''''yLibrary::returnError ---------- package returnError; use Test::Unit; require "../M''''''yLibrary.pm"; use CGI; use DBI; use IO::String; sub set_up { $error_message = "*** ERROR TEXT ***"; $M''''''yLibrary::output = new CGI (""); $M''''''yLibrary::gDBhandle = DBI->connect("DBI:mysql:mylibrary_test:localhost", \"test", "test"); } sub tear_down { $error_message = undef; $M''''''yLibrary::output = undef; $M''''''yLibrary::gDBhandle->disconnect; $M''''''yLibrary::gDBhandle = undef; } Now we can construct a test and we do a little manipulation on $expected to grab the header from $result. sub test_error_message { my $expected; my @args = ($error_message); my $result = &subroutines::trap_stdout(\&M''''''yLibrary::returnError, \@args); $expected .= 'ERROR: ' . $error_message . '
Email Eric.'; $expected .= "