in reply to How to mock Mail::Mailer

Hi,

maybe you could wrap Mail::Mailer::new and force the testfile backend:

use strict; use warnings; use Mail::Mailer; package Mail::Mailer; my $basic_new; BEGIN{ $basic_new = *Mail::Mailer::new{CODE}; } sub new($@){ my @args = @_; $args[1] = 'testfile';# override type passed in use Data::Dumper; print Dumper \@args; return $basic_new->(@args); } package main; my $m = Mail::Mailer->new('smtp'); $m->open({To => 'me@here.com'}); print $m "hi\n";

I'm not sure if this would be considered to be a sane way of doing this by the experts. So please comment if I'm suggesting something stupid...


Cheers, Christoph

Replies are listed 'Best First'.
Re^2: How to mock Mail::Mailer
by SilasTheMonk (Chaplain) on Oct 13, 2009 at 17:55 UTC

    Lamprecht,

    I don't think the idea is stupid. I have tried it and it is the closest I have got so far. In isolation it seemed to work perfectly. When I tried it in my real-life example it complained about redefining Mail::Mailer::new and the actual file was not as I expected.

    What I thought would be most likely to work to would be to take your approach of forcing a testfile and simply redefine the constructor using Test::MockObject::Extends. However I could not get that to work.

    I am sure my original approach (and perhaps the above approaches) should be workable. I will keep working on this but in the end it will simply be easiest to make my use of Mail::Mailer more configurable.