Kjell-Magne Øierud > Test-Spec-RMock-0.004 > Test::Spec::RMock

Download:
Test-Spec-RMock-0.004.tar.gz

Dependencies

Annotate this POD

View/Report Bugs
Source   Latest Release: Test-Spec-RMock-0.006

NAME ^

Test::Spec::RMock - a mocking library for Test::Spec

VERSION ^

version 0.004

SYNOPSIS ^

  use Test::Spec;
  use Test::Spec::RMock;

  describe "Something" => sub {
      it "should do something" => {
          my $foo = rmock('Foo');
          $foo->should_receive('bar')->twice->and_return('baz');
          Something->new->do_something_with($foo);
      };
  };

  runtests unless caller;

EXPORTED METHODS ^

rmock($name)

Creates a mock object with the given name.

$name is used in error messages. Often a good choice is the name of the class or role you are mocking.

USING MOCK OBJECTS ^

Method stubs

You want to use method stubs on all messages that you don't care to set expectations on. Any interactions that don't are important for the test you are writing.

$mock->stub($message_name => $return_value)

This creates a method stub on the mock object returning $return_value each time it is called.

Method mocks

Mocking methods allows you to set expectations on the messages that the mocked object should receive.

$mock->should_receive($name)
$mock->should_not_receive($name)

Null objects

Use a null object when you don't care about the object's behavior or interaction, and don't want to explicitly stub everything out that's needed.

$mock->as_null_object()

Message expectations

All return $self so that you can chain them.

$expectation->and_return(...)
$expectation->and_raise($exception)
$expectation->with(...)
$expectation->any_number_of_times()
$expectation->at_least_once()
$expectation->at_least($n)
    $expectation->at_least(4)->times
$expectation->once()
$expectation->exactly($n)
    $expectation->exactly(4)->times
$expectation->times

Noop

SEE ALSO ^

AUTHOR ^

Kjell-Magne Øierud <kjellm@cpan.org>

COPYRIGHT AND LICENSE ^

This software is Copyright (c) 2012 by Kjell-Magne Øierud.

This is free software, licensed under:

  The MIT (X11) License
syntax highlighting: