This is a small example on how to mock a database using Test::MockDBI. It allows you to test your code without relying on an actual database, and gives you fine-grained control over returned values and possible situations. Also, the dbitest flag allows one to switch between the mocked and the real database connections easily.

Mark Leighton Fisher's (the module author) article An Introduction to Test::MockDBI is well-written overview and it is worth a read.

The t and t-src directories shipped with Test::MockDBI contains more examples on how to use the module.

db.pm - the module to test:
package db; # db.pm use strict; use warnings; use Data::Dumper; use DBI; our $dbh = undef; sub connect { # The arguments to DBI->connect are ignored by Test::MockDBI $dbh = DBI->connect('DBI:mysql:test', 'foo', 'bar'); } sub disconnect { $dbh->disconnect(); } sub select_i_from_t { my $sth = $dbh->prepare(q{select i from t}) or die "prepare failed"; $sth->execute() or die "execute failed"; my $arref = []; while (my $row = $sth->fetchrow_arrayref()) { push @$arref, $row->[0]; } return $arref; } 1; __END__
db.t - the test script for db.pm
# db.t use strict; use warnings; # Signal the we want to use Test::MockDBI BEGIN { push @ARGV, "--dbitest=42"; } use Test::More; use Test::MockDBI; BEGIN { plan tests => 3 } use_ok(q{db}); # Initialize fake database my $mock_dbi = Test::MockDBI::get_instance(); # Set return values for given query (when dbitest==42) my $vals = [ [123], [124], [125] ]; $mock_dbi->set_retval_scalar( 42, "select i from t", sub { shift @$vals } # take out an array ref element ); # Connect and run query ok(defined(db::connect()), q{Expect connect() to return true}); # Check returned array ref is_deeply( db::select_i_from_t(), [ 123, 124, 125 ], q{Expect select_i_from_t() to return arrayref [ 123, 124, 125 ]} ); __END__ =pod =head1 NAME db.t - Mock a database using Test::MockDBI =head1 SYNOPSIS prove db.t =head1 DESCRIPTION This is a small example on how to mock a database using L<Test::MockDBI>. It allows you to test your code I<without relying on an actual database>, and gives you fine-grained control over returned values and possible situations. Also, the C<--dbitest=\d+> flag allows one to switch between the mocked and the real database connections easily. The C<t> and C<t-src> directories shipped with L<Test::MockDBI> contains more examples on how to use the module. =head1 SEE ALSO An Introduction to Test::MockDBI - Article by Mark Leighton Fisher, July 21, 2005 http://www.perl.com/pub/a/2005/07/21/test_mockdbi.html Test::MockDBI Test::More =head1 AUTHOR andreas1234567 =cut
--
No matter how great and destructive your problems may seem now, remember, you've probably only seen the tip of them. [1]

In reply to Testing with Test::MockDBI by andreas1234567

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.