Method Spies


 | tools+ | restore | home |

What are Method Spies?

Inserting a Spy

Editing a Spy

Spy Examples


  Method Spy Examples

The following section describes common scenarios during Smalltalk development, and illustrates how method spies can be used to assist. In particular, the examples show the use of the thisSpy pseudo variable, and the context and activationContext dictionaries.

  • You want to step-through a method which is being invoked repeatedly by a background process, however placing ‘self halt’ in the method would cause multiple walkbacks to occur. Using a method spy, it is straightforward to interrupt only the first invocation by inserting the following spy code at the start of the method: 

thisSpy activationCount = 1 ifTrue: [self halt]

This scenario is quite common and so the following shorthand form can also be used:

thisSpy isVirgin ifTrue: [self halt]

Following the initial activation, the activationCount of the spy can be reset to zero via the Reset button of the Spy Inspector.

  • To selectively halt a method if it is executed twice in succession with the same parameter:

    (thisSpy context at: #lastArgument) == anObject ifTrue: [self halt].
    context at: #lastArgument put: anObject

  • To capture the argument anObject on each invocation of a method:

thisSpy activationContext at: #argument put: anObject

…the arguments from each activation of the spy can then be examined in the Spy Inspector.


© 2002 Solutions Software Ltd.