[Pdx-pm] interesting testing hole thats been dug

Michael G Schwern schwern at gmail.com
Wed Jan 3 21:27:54 PST 2007

Wil Cooley wrote:
> It may not be the best or right way to do it, but I just put in a
> backdoor in my constructor to allow me to pass in an object of the
> expected type (and documented it as an unsupported, test-only sort of
> feature).  The module in question is basically a facade around Net::FTP:
> http://search.cpan.org/src/WILCO/Net-FTP-Simple-0.0006/lib/Net/FTP/Simple.pm
> (My constructor is private and called "_new()".)
> Then I create Test::MockObject objects for my tests:
> http://search.cpan.org/src/WILCO/Net-FTP-Simple-0.0006/t/10-Net-FTP-Simple.t

That's going in the right direction, allowing users to pass in elements of your object's composition rather than hard coding it all.  Why not make that option available publicly?  That way they can use a different subclass of Net::FTP.  Or pass in a differently configured Net::FTP object.  Or a mock object for testing, just like you're doing.

While testing, if you find your existing interface inadequate chances are your users will, too.  That's why its better to add to the public interface rather than put in private testing hacks.

More information about the Pdx-pm-list mailing list