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.t - the test script for db.pmpackage 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 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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Testing with Test::MockDBI
by CaMelRyder (Pilgrim) on May 07, 2010 at 20:12 UTC |