in reply to Re^2: Mocking with namespace::autoclean and Moose::Exporter
in thread Mocking with namespace::autoclean and Moose::Exporter
but I hadn't tested it.#!/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); }
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 }
|
|---|