Barton Chittenden
Barton Chittenden

Reputation: 4416

Arguments not being passed correctly to Test::MockObject method

I'm working on setting up a test script in Perl. I'm using Test::MockObject to create a mock object which will hold some configuration data. The configuration in the live program comes from an INI file, i.e. it has the format

[SECTION]
KEY = VALUE

As such, I've set up the following:

use Test::MockObject;
use Data::Dumper qw(Dumper);

use constant SECTION_NAME => 'section';
use constant KEY_NAME => 'key';
use constant VALUE_NAME => 'value';

my $com_mock = Test::MockObject->new();

$com_mock->mock( 'getIniVar', sub {
   my $self = shift;
   my ( $section, $key ) = @_;
   print STDERR "\$_[0] = '" . Dumper( $_[0] ) ." '\n";
   print STDERR "\$_[1] = '" . Dumper( $_[1] ) ." '\n";  
   my %iniVar = ( SECTION_NAME => { KEY_NAME => VALUE_NAME } );
   return( $iniVar{$section}->{$key} );
} );

$self->{com} = $com_mock;

Later, I actually call the mocked function:

print STDERR
      "\$self->{com}->getIniVar( 'section', 'key') = '"
    . $self->{com}->getIniVar( SECTION_NAME,KEY_NAME )
    . "'\n";

When I run the test, I see the following:

ok 1 - use Appriss::ImageExtraction3::Config;
$_[0] = '$VAR1 = 'section';
 '
$_[1] = '$VAR1 = 'key';
 '
Use of uninitialized value in concatenation (.) or string at t/config/config.pm line 159.
$self->{com}->getIniVar( 'section', 'key') = ''
$_[0] = '$VAR1 = undef;
 '
$_[1] = '$VAR1 = 'key';
 '

The first time that the mock object is called is during

use_ok 'Appriss::ImageExtraction3::Config';

This seems to have all of the arguments that I want, but I'm not explicitly calling that, so I don't know what's happening there. The second time is when I explicitly call it using

$self->{com}->getIniVar( SECTION_NAME,KEY_NAME )

(Shown above)... and at this point, the first argument is set to undef... why?

Upvotes: 0

Views: 327

Answers (1)

Dallaylaen
Dallaylaen

Reputation: 5308

This line looks suspicious:

my ( $section, $key ) = @_;

As I understand, the first argument should be the mock object itself.

As for P.S., you can run t/*.t files individually:

% perl -Ilib -d t/13-unlucky.t

Upvotes: 1

Related Questions