#! /usr/bin/perl use strict; use Test::LectroTest; use Test::MockObject; use List::Util qw( reduce ); use Score; BEGIN { # Faking the database component Test::MockObject->fake_module( 'My::Database' ); } # Making a mockobject of the database my $mock = Test::MockObject->new(); $mock->fake_new( 'My::Database' ); my $scorer = Score->new(); # The Score module expects the database results as hashes. # This closure generator allows me to mock the database # access method. sub hash_sequence { my @sequence = @_; my $index = 0; return sub { return %{ $sequence[ $index++ ] }; } } Property { ##[ input <- List( Int( range=>[50,100], sized=>1), length=>5 ) ]## # Calculate some 'constants' my $questions = reduce { $a + $b } @{ $input }; # Sum the result set my @data; for( my $i = 0; $i < 5; $i++ ) { # Feed the mocker some mock push( @data, { 1 => $input->[ $i ] } ); } # Setup mockobject $mock->mock( 'getHash', &hash_sequence( @data ) ); $scorer->setDatabase( $mock ); # Calculate expected result my $raw = ( ( $input->[ 2 ] + ( 2 * $input->[ 3 ] ) - $input->[ 0 ] ) + $questions ) / ( 3 * $questions ); my %result = $scorer->score(); # The method to test $raw == $result{ 1 }; }, name => "score holds.";