in reply to Re: Mocking with namespace::autoclean and Moose::Exporter
in thread Mocking with namespace::autoclean and Moose::Exporter

Please, try to show more than just a snippet. The exact placement of it might be important. I wasn't able to make it work: without use Logger;, it fails with
Cannot replace non-existent sub (Logger::log_warn) at t/01-basic.t lin +e 18. BEGIN failed--compilation aborted at t/01-basic.t line 18.

while with it, it fails a bit later with

1..4 ok 1 - constructs ok 2 - no warnings ok 3 - constructs Undefined subroutine &MyObj::log_warn called at /home/choroba/_/0/lib/ +MyObj.pm line 11.

Which is exactly why I asked the original question.

map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]

Replies are listed 'Best First'.
Re^3: Mocking with namespace::autoclean and Moose::Exporter
by NERDVANA (Priest) on Nov 15, 2024 at 23:14 UTC
    This is what I had in mind:
    #!/usr/bin/perl use warnings; use strict; use Test::More tests => 2 * 2; use Sub::Override; my @warnings; my $override; use Logger; BEGIN { $override = 'Sub::Override'->new( 'Logger::log_warn' => sub { push @warnings, $_[0] } ); } use MyObj; for my $test ([1, undef, 'no warnings'], [11, 'Value too large', 'warn +ings']) { my ($value, $warnings, $name) = @$test; @warnings = (); my $o = bless {value => $value}, 'MyObj'; ok($o, 'constructs'); $o->foo; is($warnings[0], $warnings, $name); }
    but I hadn't tested it. It does in fact fail, but only because you have a bug that needs fixed in the code being tested :-)

    Edit: Turns out when you replace the Logger sub, Moose::Exporter can't determine the sub name from the coderef. Export it like this and it works:

    my ($import, $unimport, $init_meta) = Moose::Exporter->build_import_me +thods( as_is => ['Logger::log_warn'] ); sub import { goto &$import }