Jasmine::Spy
version 1.02
use Test::Spec; use Jasmine::Spy qw(spyOn stopSpying expectSpy); describe "FooClass" => sub { before each => sub { spyOn("BarClass", "bazMethod")->andReturn("Bop"); spyOn("BarClass", "bam")->andCallThrough; }; it "calls BarClass" => sub { FooClass->doTheThing(); expectSpy("BarClass", "bazMethod")->toHaveBeenCalled(); }; it "calls BarClass->bat method with 'bam'" => sub { FooClass->doTheThing(); expectSpy("BarClass", "bat")->toHaveBeenCalledWith('bam') }; after each => sub { stopSpying("BarClass"); }; };
Nothing is exported by default, but they cann all be pulled in with the :all tag
This is the setup method to begin spying. $invocant may be either an object instance or the name of a class. Spying on a Class will automatically spy on all instances of the class, even those created before setting up the spy. Spyng on an instance only effects that instance, not the class or other instances of that class.
A "spy" object is returned from this call which will allow introspection and testing of calls. However there is no need to catch this, as other convience methods provide a better way of performing the same introspection later.
Use this call to stop spying and restore original functionality to the object or class.
Use this to retrieve the "spy" object created by spyOn. It also sets the spy object to introspect of the provided $method. There is only one spy object created for each distinct $invocant beign spied on, even if multiple methods are being watched. This is why expectSpy is the recomended way to start introspection on a spied method.
$method
expectSpy
This will fetch an array of array's containing the arguments passed each time the $method was called. This is a tied array ref which also provides convience methods first and mostRecent.
first
mostRecent
Test that the spied method has been called atleast once.
Test that the spied method was never called.
Expects that the spied method has been called with arguments matching $matchers atleast once. This is done with deep comparison via Test::Deep.
$matchers
Inverse of toHaveBeenCalledWith.
Sets the spied method to return the supplied value. Usually this would be called directly on the return from spyOn.
spyOn
For example:
spyOn($foo, 'bar')->andReturn('baz')
Sets the spied method to call through to the original method, recording arguments passed along the way.
Sets the spied method to invoke the supplied code reference in place of the original method. It does also record the arguments along the way.
Quantifiers may be called on the return of either toHaveBeenCalled or toHaveBeenCalledWith to stipulate how many times the method should have been called.
toHaveBeenCalled
toHaveBeenCalledWith
The method was called exactly one time
Method was called atleast X times
Method was not called more than X times
Method was called exactly X times.
Having put some thought into this, I haven't come up with a clean robust way to handle it. In the end, I think you are better off using andCallFake to throw an exception if you need to test that.
andCallFake
Test::Spec, Test::Deep
To install Jasmine::Spy, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Jasmine::Spy
CPAN shell
perl -MCPAN -e shell install Jasmine::Spy
For more information on module installation, please visit the detailed CPAN module installation guide.