Your browser doesn't support the features required by impress.js, so you are presented with a simplified version of this presentation.

For the best experience please use the latest Chrome or Safari browser. Firefox 10 (to be released soon) will also handle it.

データベースのテストのお話::Lite

@tsucchi

自己紹介

で、今回のテーマについて

まあいいや

特にあんまりネタ無いので、既出のやつから

最近のお仕事の話

で、やったこと

それ、DBD::Mock でよくね?

それ、DBD::Mock でよくね?

じゃあ、Test::Mock::Guard(とか)で、DBI の振る舞い偽装すればよくね?

じゃあ、Test::Mock::Guard(とか)で、DBI の振る舞い偽装すればよくね?

つーわけで作った

使い方(1) $sth をなんとかするやつ

use Test::Stub::DBI;
my $count = 0;
my $guard = stub_dbi(
    sth => {
        fetchrow_arrayref => sub { 
            $count++;
            return [0] if ( $count == 1 );
            return;
        }
    },
);
my $dbh = Test::Stub::DBI->connect();
my $sth = $dbh->prepare('SELECT * FROM SOME_TABLE');
$sth->execute();
is_deeply( $sth->fetchrow_arrayref, [0] );
is( $sth->fetchrow_arrayref, undef );

使い方(2) $dbh をなんとかするやつ

my $count = 0;
my $guard = stub_dbi(
    dbh => { prepare => sub { die "prepare failed" }, },
);
my $dbh = Test::Stub::DBI->connect();
try {
    my $sth = $dbh->prepare('SELECT * FROM SOME_TABLE');
    fail 'exception expected';
} catch {
    like( $_, qr/^prepare failed/ );
};

使い方(3) DBI をなんとかするやつ

my $count = 0;
my $guard = stub_dbi(
    dbi => {
        connect => sub { die "connect failed" },
    },
);
try {
    my $dbh = DBI->connect('dbd::dummy');
    fail 'exception expected';
} catch {
    like( $_, qr/^connect failed/ );
};

注意

おしまい

Use a spacebar or arrow keys to navigate