- Overview
-
Mocking Library
Test::Stream comes with a very powerful mocking library.
- Based off of Mock::Quick's good ideas
- Fixes API issues from Mock::Quick
- Scoped Mocking
- Adding and Overriding are seperate
- Classes
-
Mocking Classes
You can mock a class directly:
You can also mock a class as gathered from an instance of the class:
- Objects
-
Mocking Objects
- A mocked object is an object with an anonymous class.
- Methods are typically auto-vivified as read/write accessors.
- You can add/remove methods as needed.
- Great for shoving into a place that just needs any object.
- $mock
-
$mock Object Methods
The $mock object itself has several useful methods:
- add
- Add a symbol
- override
- override a symbol
- restore
- restore a symbol (pop)
- reset
- reset symbol to original
- reset_all
- undo all mocking
- orig
- get the original symbol
- current
- get the current symbol
- before
- new sub that calls custom code, then the original
- after
- new sub that calls original sub, then your custom code
- around
- new sub that calls your custom sub with the original as an arg
- autoload
- turn on autovivification of accessors
- block_load
- prevent the real class from being loaded
- file
- return the filename for the mocked class
- stash
- get a reference to the mocked package stash
- parent
- get the parent mock object if there is nested mocking
- child
- get the child mock object if there is nested mocking
- Spec
-
Mock + Spec
- This plugin plays nicely with the Spec plugin.
- Mocks are treated like a before_each in void context.
- Mocks apply to the surrounding describe as well.