Test::ExpectAndCheck::Future - expect/check-style unit testing with Future-returning methods
Test::ExpectAndCheck::Future
expect/check
Future
use Test::More; use Test::ExpectAndCheck::Future; my ( $controller, $puppet ) = Test::ExpectAndCheck::Future->create; { $controller->expect( act => 123, 45 ) ->returns( 678 ); is( $puppet->act( 123, 45 )->get, 678, '$puppet->act yields result' ); $controller->check_and_clear( '->act' ); } done_testing;
This package creates objects that assist in writing unit tests with mocked object instances. Each mocked "puppet" instance will expect to receive a given list of method calls. Each method call is checked that it received the right arguments, and will return a Future instance to yield the prescribed result. At the end of each test, each object is checked to ensure all the expected methods were called.
It is a variation of Test::ExpectAndCheck, assistance around the results of invoked methods. Every invoked method will return a Future instance. The "returns" or "throws" method can then set the desired eventual result of that future instance for each expectation.
These return instances are implemented using Test::Future::Deferred, so they are not immediately ready. Instead they will only become ready after a toplevel await expression or call to the get method. This should help unit tests to run similarly to real-world behaviour, where most futures returned by real-world interfaces (such as IO systems) would not be immediately ready. This behaviour can be switched off for individual expectations by using the "immediately" method.
await
get
$exp->returns( @result )
Sets the result that the future returned by this method call will yield.
$exp->fails( $message ) $exp->fails( $message, $category, @details )
Sets the failure that the future returned by this method call will yield.
$exp->returns( ... )->immediately $exp->fails( ... )->immediately
Switches this expectation to return an immediate future, rather than a deferred one.
Paul Evans <leonerd@leonerd.org.uk>
To install Test::ExpectAndCheck, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Test::ExpectAndCheck
CPAN shell
perl -MCPAN -e shell install Test::ExpectAndCheck
For more information on module installation, please visit the detailed CPAN module installation guide.