|
Lines 5-14
Link Here
|
| 5 |
|
5 |
|
| 6 |
use strict; |
6 |
use strict; |
| 7 |
use warnings; |
7 |
use warnings; |
| 8 |
|
8 |
use DBI; |
| 9 |
use Test::More tests => 1; |
9 |
use Test::More tests => 6; |
|
|
10 |
use Test::MockModule; |
| 10 |
|
11 |
|
| 11 |
BEGIN { |
12 |
BEGIN { |
| 12 |
use_ok('C4::ItemType'); |
13 |
use_ok('C4::ItemType'); |
| 13 |
} |
14 |
} |
| 14 |
|
15 |
|
| 15 |
- |
16 |
|
|
|
17 |
my $module = new Test::MockModule('C4::Context'); |
| 18 |
$module->mock('_new_dbh', sub { |
| 19 |
my $dbh = DBI->connect( 'DBI:Mock:', '', '' ) |
| 20 |
|| die "Cannot create handle: $DBI::errstr\n"; |
| 21 |
return $dbh }); |
| 22 |
|
| 23 |
# Mock data |
| 24 |
my $itemtypes = [ |
| 25 |
['itemtype','description','rentalcharge','notforloan','imageurl','summary'], |
| 26 |
['BK','Books',0,0,'',''], |
| 27 |
['CD','CDRom',0,0,'',''] |
| 28 |
]; |
| 29 |
|
| 30 |
|
| 31 |
my $dbh = C4::Context->dbh(); |
| 32 |
|
| 33 |
my @itemtypes = C4::ItemType->all(); |
| 34 |
is(@itemtypes, 0, 'Testing all itemtypes is empty'); |
| 35 |
|
| 36 |
# This should run exactly one query so we can test |
| 37 |
my $history = $dbh->{mock_all_history}; |
| 38 |
is(scalar(@{$history}), 1, 'Correct number of statements executed') ; |
| 39 |
|
| 40 |
|
| 41 |
# Now lets mock some data |
| 42 |
$dbh->{mock_add_resultset} = $itemtypes; |
| 43 |
|
| 44 |
@itemtypes = C4::ItemType->all(); |
| 45 |
is(@itemtypes, 2, 'ItemType->all should return an array with 2 elements'); |
| 46 |
|
| 47 |
is($itemtypes[0]->description, 'Books', 'First itemtype is books'); |
| 48 |
|
| 49 |
is($itemtypes[0]->fish,undef,'Calling a bad descriptor gives undef'); |