Test::ExpectAndCheck::Future - expect/check-style unit testing with Future-returning methods


   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' );



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.



   $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.



Sets that the future returned by this method will not complete and simply remain pending.


Paul Evans <>