in reply to Mocking Classes and Test::Class
Is there a way to make this work?
I find that mucking around with the symbol table like this with persistent test environments is just more trouble than it's worth. Maybe try using Hook::LexWrap instead?
use strict; use warnings; { package X; use base qw(Test::Class); use Test::More; use DateTime; use Hook::LexWrap; sub setup_test_double : Test( startup ) { my ( $self ) = @_; # we stick our Hook::LexWrap object in $self so it'll # magically go out of scope after this test class has # run, so get garbage collected, so return DateTime to # its pristine state $self->{ test_double } = wrap 'DateTime::now', pre => sub { $_[-1] = 'XXXX' }; } sub use_test_double : Test { my ( $self ) = @_; is( DateTime->now, 'XXXX' ); } } { package Y; use base qw(Test::Class); use Test::More; use DateTime; sub using_real_class : Test { my ( $self ) = @_; isa_ok( DateTime->now, 'DateTime' ); } } Test::Class->runtests;
|
|---|