From acde5f72b17e02aae7c427f8a0a3a2e420348073 Mon Sep 17 00:00:00 2001 From: Martin Renvoize Date: Fri, 23 Jun 2023 13:16:44 +0100 Subject: [PATCH] Bug 30524: Attempt to mock --- t/Output.t | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/t/Output.t b/t/Output.t index c9b202f3dc..79a7f212c3 100755 --- a/t/Output.t +++ b/t/Output.t @@ -19,7 +19,7 @@ use Modern::Perl; use Test::More tests => 8; use Test::Warn; -use Test::Trap; +use Test::MockModule; use File::Temp qw/tempfile/; use CGI qw ( -utf8 ); @@ -103,6 +103,18 @@ subtest 'output_and_exit_if_error() tests' => sub { plan tests => 2; + my $exit; + my $output = Test::MockModule->new('C4::Output'); + $output->mock( + 'output_and_exit', + sub { + my ( $query, $cookie, $template, $error ) = @_; + $template->param( blocking_error => $error ); + output_html_with_http_headers( $query, $cookie, $template->output ); + $exit = 1; + } + ); + t::lib::Mocks::mock_config( 'pluginsdir', [ C4::Context::temporary_directory ] ); my ( $fh, $fn ) = tempfile( SUFFIX => '.tt', UNLINK => 1, DIR => C4::Context::temporary_directory ); print $fh qq|[% blocking_error %]|; @@ -119,7 +131,12 @@ subtest 'output_and_exit_if_error() tests' => sub { } ); - my @r = trap { output_and_exit_if_error($query, $cookie, $template, { check => 'csrf_token' }) }; - is ( $trap->exit, 0, 'Expecting &output_and_exit_if_error to exit with 0' ); - like ( $trap->stdout, qr/wrong_csrf_token/, 'Expecting wrong_csrf_token' ); + local *STDOUT; + my $stdout; + + open STDOUT, '>', \$stdout; + output_and_exit_if_error($query, $cookie, $template, { check => 'csrf_token' }); + is( $exit, 1, "Exit"); + like ( $stdout, qr/wrong_csrf_token/, 'Expecting wrong_csrf_token' ); + close STDOUT; }; -- 2.41.0