Practical Test-driven Development Presentation
.pdfCode That Accesses Databases
•Some options
•Run the tests in a virtual machine
•Mock the database interaction layer
•Use a lightweight database such as SQLite
191
Mocking the Database Access Layer
Case Studies
“t/test_with_dbi_mock.t”
1 use warnings;
2 use strict;
3 use Test::More qw(no_plan);
4
5 BEGIN { use_ok( 'PrimeNumberIterator' ) };
192
Mocking the Database Access Layer
Case Studies
--(0)> prove -Ilib t/test_with_dbi_mock.t
t/test_with_dbi_mock....
#Failed test 'use PrimeNumberIterator;'
#at t/test_with_dbi_mock.t line 5.
#Tried to use 'PrimeNumberIterator'.
#Error: Can't locate PrimeNumberIterator.pm in @INC (@INC contains: ....) at (eval 3) line 2.
# BEGIN failed--compilation aborted at t/test_with_dbi_mock.t line 5.
# Looks like you failed |
1 test of 1. |
t/test_with_dbi_mock.... |
dubious |
Test returned status 1 (wstat 256, 0x100) |
|
DIED. FAILED test 1 |
|
Failed 1/1 tests, 0.00% okay |
|
Failed Test |
Stat Wstat Total Fail List of Failed |
-------------------------------------------------------------------------------
t/test_with_dbi_mock.t |
1 256 |
1 |
1 1 |
|
Failed |
1/1 test scripts. 1/1 subtests |
failed. |
||
Files=1, Tests=1, 0 wallclock secs ( |
0.02 |
cusr + 0.01 csys = 0.03 CPU) |
||
Failed |
1/1 test programs. 1/1 subtests failed. |
193
Mocking the Database Access Layer
Case Studies
“lib/PrimeNumberIterator.pm”
1 package PrimeNumberIterator;
2
3 use warnings;
4 use strict;
5
6 1;
194
Mocking the Database Access Layer
Case Studies
--(0)> prove -Ilib t/test_with_dbi_mock.t t/test_with_dbi_mock....ok
All tests successful.
Files=1, Tests=1, 1 wallclock secs ( 0.02 cusr + 0.01 csys = 0.03 CPU)
195
Mocking the Database Access Layer
Case Studies
“t/test_with_dbi_mock.t”
1 use warnings;
2 use strict;
3 use Test::More qw(no_plan);
4
5 BEGIN { use_ok( 'PrimeNumberIterator' ) };
6
7 can_ok( 'PrimeNumberIterator', qw(new) );
196
Mocking the Database Access Layer
Case Studies
--(0)> prove -Ilib t/test_with_dbi_mock.t
t/test_with_dbi_mock....
#Failed test 'PrimeNumberIterator->can('new')'
#at t/test_with_dbi_mock.t line 7.
#PrimeNumberIterator->can('new') failed
#Looks like you failed 1 test of 2.
t/test_with_dbi_mock |
....dubious |
Test returned status 1 (wstat 256, 0x100) |
|
DIED. FAILED test 2 |
|
Failed 1/2 tests, 50.00% okay |
|
Failed Test |
Stat Wstat Total Fail List of Failed |
-------------------------------------------------------------------------------
t/test_with_dbi_mock.t |
1 256 |
2 |
1 2 |
|
Failed |
1/1 test scripts. 1/2 subtests |
failed. |
||
Files=1, Tests=2, 0 wallclock secs ( |
0.02 |
cusr + 0.01 csys = 0.03 CPU) |
||
Failed |
1/1 test programs. 1/2 subtests failed. |
197
Mocking the Database Access Layer
Case Studies
“lib/PrimeNumberIterator.pm”
1 package PrimeNumberIterator;
2
3 use warnings;
4 use strict;
5
6 sub new {}
7
8 1;
198
Mocking the Database Access Layer
Case Studies
--(0)> prove -Ilib t/test_with_dbi_mock.t t/test_with_dbi_mock....ok
All tests successful.
Files=1, Tests=2, 1 wallclock secs ( 0.02 cusr + 0.01 csys = 0.03 CPU)
199
Mocking the Database Access Layer
Case Studies
“t/test_with_dbi_mock.t”
5 BEGIN { use_ok( 'PrimeNumberIterator' ) };
6
7 can_ok( 'PrimeNumberIterator', qw(new) );
8
9 my $p = PrimeNumberIterator->new();
10 isa_ok( $p, 'PrimeNumberIterator' );
200