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

Invoked with perl -wc, both test and module say 'syntax OK'. This seems so simple. I'm apparently missing something pretty obvious here. Any ideas what that might be?

Here is what I've got:

1..2 ok 1 - use YMD::WWW::DB; not ok 2 - YMD::WWW::DB->can(...) # Failed test 'YMD::WWW::DB->can(...)' # at t/08-db.t line 14. # YMD::WWW::DB->can('We can access the ->connect() method') failed # Testing YMD::WWW::DB 0.01, Perl 5.008008, /var/usr/bin/perl # Looks like you failed 1 test of 2.
So says my simple test file. The module it is supposed to be exercising says this:

package YMD::WWW::DB; use warnings; use strict; use DBI; use Data::Dumper; sub connect { my $self = shift; my $db = shift; # print STDERR "->connect() says that \$db is: " . Dumper(\$db); my $db_driver = $db->{'db_driver'}; my $host_name = $db->{'db_host'}; my $db_name = $db->{'db_name'}; my $USER = $db->{'db_user'}; my $PASSWORD = $db->{'db_pw'}; my $dsn = "DBI:$db_driver:host=$host_name;database=$db_name"; if($db->{'db_ssl'}){ $dsn .= ';sslmode=require'; } # print $dsn,"\n"; return (DBI->connect($dsn,$USER,$PASSWORD, {PrintError => 0, RaiseError => 1})); } 1; # End of YMD::WWW::DB
and the test itself reads:

#!perl -T use Test::More tests => 2; use lib qw{/home/hesco/sandbox/YMD-WWW-SamplePhoneCall/lib}; BEGIN { use_ok( 'YMD::WWW::DB' ); } use YMD::WWW::DB; can_ok('YMD::WWW::DB','connect','We can access the ->connect() method' +); diag( "Testing YMD::WWW::DB $YMD::WWW::DB::VERSION, Perl $], $^X" );
I would think that my module->can('method'), even if I haven't yet built the config hash to pass it on its interface. Any clues what I might be missing here, please?

-- Hugh

UPDATE:

Thanks folks, that was exactly it. I found and corrected three other bugs in this code simply preparing this post, but it never occurred to me to perldoc Test::More for the can_ok function. I just assumed I knew how to use that one. Thanks again. I'll get back to work on this project after getting some groceries in the house.

if( $lal && $lol ) { $life++; }

Replies are listed 'Best First'.
Re: Can't find my method . . .
by kyle (Abbot) on Nov 07, 2008 at 20:33 UTC

    The documentation for Test::More says that can_ok() is used this way:

    can_ok($module, @methods);

    That is, it doesn't take a test name. Change your invocation to this:

    can_ok( 'YMD::WWWW::DB', 'connect' );
Re: Can't find my method . . .
by gwadej (Chaplain) on Nov 07, 2008 at 20:38 UTC

    I've been bitten by this before.

    The can_ok method does not support a message. It takes a list of methods to test. The message is being treated as a method name, which obviously fails.

    can_ok('YMD::WWW::DB','connect');

    should do what you want.

    G. Wade
Re: Can't find my method . . .
by ikegami (Patriarch) on Nov 07, 2008 at 20:37 UTC

    can_ok($module, @methods). "We can access the ->connect() method" is being treated as the name of a method. Use

    can_ok('YMD::WWW::DB','connect')

    or

    ok( YMD::WWW::DB->can('connect'), 'We can access the ->connect() method' );

    The former is probably better.