A teachable object for Pharo.
Using the small project you can easily create objects responding to specific messages. This is useful for mocking in unit tests and other. The code goes back to an early idea of Ernest Micklei with an implementation for Pharo by Torsten Bergmann (astares).
Metacello new repository: 'github://astares/Pharo-Teachable/src'; baseline: 'Teachable'; load
It's an implementation of a Teachable class who's instances can be taught to respond to messages. It's usefull for creating mocks who should behave like other objects (for instance inside of a test case) without actually implementing a real mock class. Here is an example how it can be used:
|teachable| teachable := Teachable new. teachable whenSend: #help return: 'ok'; whenSend: #doit evaluate: [1 inspect]; acceptSend: #noDebugger; whenSend: #negate: evaluate: [:num | num negated].
After teaching the object we can use it as if it had a normal implementation in a class:
teachable help. "this will return the string 'ok'" teachable doit. "this will open the inspector on the SmallInteger 1" teachable noDebugger. "this will accept the send of #noDebugger and return the teachable" teachable negate: 120 "this will return -120"