zby has asked for the wisdom of the Perl Monks concerning the following question:

I've just decided to try Test::MockObject for my tests but I have problems with it. The error message seems to indicate that the subroutine encoding_from_html_document is not exported by HTML::Encoding - but in the BEGIN block I try to mock this subroutine (perhaps the double "" is indication of some internal bug, but I am a novice here). Here is my code:
use Test::More tests => 1; BEGIN { use Test::MockObject; Test::MockObject->fake_module ( 'HTML::Encoding', encoding_from_html_document => sub{ 'iso' } ); } use_ok( Catalyst::Test, 'LinkDB' );
And here is the error message when I try to run it:
1..1 not ok 1 - use Catalyst::Test; # Failed test (t/m/cdbi_link1.t at line 11) # Tried to use 'Catalyst::Test'. # Error: Couldn't load "LinkDB", "Couldn't load "LinkDB::C::Bookm +ark", ""encoding_from_html_document" is not exported by the HTML::Enc +oding module # Can't continue after import errors at lib/LinkDB/C/Bookmark.pm line +9 # BEGIN failed--compilation aborted at t/m/cdbi_link1.t line 11. # Compilation failed in require at /usr/local/share/perl/5.8.4/Module/ +Pluggable/Fast.pm line 66. # " at /usr/local/share/perl/5.8.4/Module/Pluggable/Fast.pm line 68. # Compilation failed in require at /usr/local/share/perl/5.8.4/Catalys +t/Test.pm line 78. # " at /usr/local/share/perl/5.8.4/Catalyst/Test.pm line 80. # BEGIN failed--compilation aborted at (eval 5) line 2. # Looks like you failed 1 test of 1.
What is wrong here?

I need to add that without the BEGIN block the test runs OK.

This is Test::MockObject VERSION = '0.20' and Perl version 5.8.4.

Replies are listed 'Best First'.
Re: Test::MockObject and mocking module subroutines
by chromatic (Archbishop) on Jun 22, 2005 at 16:26 UTC

    fake_module() prevents Perl from loading HTML::Encoding, so if its import() does anything that the code your testing needs, you have to do it yourself.

    One option is to localize *HTML::Encoding::encoding_from_html_document or the glob in the appropriate package and assign your own subroutine to it there. I don't remember at the moment what Test::MockModule does, but it might handle this for you better than T::MO does.