File: 101-set-fake-retval-coderef.t

package info (click to toggle)
libtest-mockdbi-perl 0.70-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 372 kB
  • sloc: perl: 954; makefile: 2
file content (46 lines) | stat: -rw-r--r-- 1,457 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
use strict;
use warnings;
use Test::More;

use_ok('Test::MockDBI');

my $instance = Test::MockDBI::get_instance();

#Test all methods with a coderef retval

my %methods = (
  'DBI::db' => ['prepare', 'prepare_cached', 'do', 'commit', 'rollback', 'begin_work', 'ping', 'disconnect'],
  'DBI::st' => ['bind_param', 'bind_param_inout', 'execute', 'fetchrow_arrayref', 'fetchrow_array', 'fetchrow_hashref',
                'fetchall_arrayref', 'finish', 'rows']
);

my $dbh = DBI->connect('DBI:mydb:somedb', 'user1', 'password1', { AutoCommit => undef }); #AutoCommit => undef to silence warnings!
my $sth = $dbh->prepare('SELECT something FROM sometable');
$sth->execute(); #Make sure its executed

{
  
  #Testing the databasehandler
  foreach my $method ( @{ $methods{'DBI::db'} } ){
    #Setting a fake retval for the prepare method
    $instance->set_retval( method => $method, retval => sub {
      return "The returnvalue";
    });
    my $retval = $dbh->$method();
    cmp_ok($retval, 'eq', 'The returnvalue', $method . ' returned \'The returnvalue\'');
    
  }
  
  #Testing the statementhandler
  foreach my $method ( @{ $methods{'DBI::st'} } ){
    #Setting a fake retval for the prepare method
    $instance->set_retval( method => $method, retval => sub {
      return "The returnvalue";
    });
    my $retval = $sth->$method();
    cmp_ok($retval, 'eq', 'The returnvalue', $method . ' returned \'The returnvalue\'');
    
  }  
}

done_testing();