作为某些测试的一部分,我希望暂时将消息的内容替换为将返回一组可预测的测试值的内容。完成此类工作的标准 Smalltalk-y 方式是什么?是否有一些示例代码供我查看?
一些澄清:
这是我要编写的一些伪代码:
replaceMessage: 'fibonacci'
onClass: 'funFunctions'
with: 'returnsPredictableNumbersWithoutCalculatingThem'.
self runTestSet1.
restoreMessage: 'fibonacci'
onClass: 'funFunctions'.
self runFollowUpSet1. "Depends on state set by Set1"
replaceMessage: 'fibonacci'
onClass: 'funFunctions'
with: 'returnsPredictableNumbersWithoutCalculatingThemPart2'.
self runFollowUpSet2. "Depends on state set by followupset1"
restoreMessage: 'fibonacci'
onClass: 'funFunctions'.
最佳答案
要将一条消息替换为另一条消息,您可以:
a) 在相应的方法中,更改负责给定消息发送的选择器。
b) 使用代理来包装所有感兴趣的对象并拦截给定的消息,以便使用与原始方法不同的评估路径。
关于metaprogramming - 如何在 Smalltalk 中以编程方式替换消息正文?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4762058/