Hello.

I'm trying to test my DBI based application with the help of Test::MockDBI module. And--I don't get it.

I have the following code:

→ File dblibtry.t

#!/usr/bin/env perl use strict; use warnings; use diagnostics; use Readonly; use Test::MockDBI; use Test::Simple tests => 10; use DbLibTry; # Testing DbLibTry Perl module Readonly my %RET_VALS => ( 1 => { name => 'fgv3df1fv5f3', time => '32:20', album => 5120, order => 2, }, 2 => { name => 'fdgetre14w6e46we4', time => '2:00', album => 89, order => 5, }, ); my $dblibtry = DbLibTry->new; my $mock_dbi = Test::MockDBI::get_instance; my %song; $mock_dbi->set_retval( method => 'get_all_songs', retval => \%RET_VALS ); ok(defined $dblibtry, "Defined $dblibtry"); ok($dblibtry->isa('DbLibTry'), 'ISA'); ok($dblibtry->can('connect'), 'Method ->connect() exists'); ok($dblibtry->can('disconnect'), 'Method ->disconnect() exists'); ok($dblibtry->can('get_all_songs'), 'Method ->get_all_songs() exists') +; ok($dblibtry->connect == 1, 'Connected'); ok(%song = %{$dblibtry->get_all_songs}); ok($dblibtry->disconnect == 1, 'Disconnected'); ok($song{2}->{order} == 2); ok($song{1}->{time} eq '2:00');

→ File DbLibTry.pm

package DbLibTry; use strict; use warnings; use diagnostics; use DBI; sub new { return bless {}, shift; } sub connect { my $self = shift; $self->{dbh} = DBI->connect( "DBI:SQLite:dbname=songdb.sqlite3", q{}, q{}, {RaiseError => 1} ) or return $DBI::errstr; return 1; } sub disconnect { my $self = shift; $self->{dbh}->disconnect; return 1; } sub get_all_songs { my $self = shift; my $sth = $self->{dbh}->prepare('select * from songs;'); my $rv = $sth->execute or return $DBI::errstr; return $DBI::errstr if ($rv < 0); my %hash; while (my @row = $sth->fetchrow_array) { $hash{$row[0]} = { name => $row[1], # Name of song time => $row[2], # Total time of song album => $row[3], # Album ID for song order => $row[4], # Order of song in album }; } return \%hash; } 1;

→ File dblibtry.pl

#!/usr/bin/env perl use strict; use warnings; use diagnostics; use feature qw(say); use Data::Dumper; use DbLibTry; my $dblibtry = DbLibTry->new; $dblibtry->connect; my %hash = %{$dblibtry->get_all_songs}; $dblibtry->disconnect; say Dumper %hash;

If I run the .pl file, everything is fine, but if I run the test, I got the following output:

$ perl dblibtry.t 1..10 ok 1 - Defined DbLibTry=HASH(0x8847860) ok 2 - ISA ok 3 - Method ->connect() exists ok 4 - Method ->disconnect() exists ok 5 - Method ->get_all_songs() exists ok 6 - Connected Can't use an undefined value as a HASH reference at dblibtry.t line 46 + (#1) (F) A value used as either a hard reference or a symbolic referenc +e must be a defined value. This helps to delurk some insidious errors. Uncaught exception from user code: Can't use an undefined value as a HASH reference at dblibtry.t lin +e 46. # Looks like you planned 10 tests but ran 6. # Looks like your test exited with 255 just after 6. $

What am I doing wrong?


In reply to Test::MockDBI example [SOLVED] by brilant_blue

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.