The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

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

VERSION

version 0.006

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(%spec)

This creates method stubs for each message defined in %spec.

  $mock->stub(
      message1 => 'foo',
      message2 => 'bar',
  );
  $mock->message1; # 'foo'
  $mock->message2; # 'bar'
$mock->stub_chain(@method_names)

Creates a chain of two or more method stubs in one statement.

  $mock->stub_chain(qw(one two three))->and_return('four');
  $mock->one->two->three; # 'four'

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