Fwd: Fowler on Mocks

Zulq Alam zulq at orange.net
Tue Aug 8 21:24:53 UTC 2006


Skipped content of type multipart/alternative-------------- next part --------------
'From Squeak3.9alpha of 4 July 2005 [latest update: #7048] on 8 August 2006 at 10:22:52 pm'!
Object subclass: #MockObject
	instanceVariableNames: 'answers answerWithMockAnswer'
	classVariableNames: ''
	poolDictionaries: ''
	category: 'Mock-Prototype'!
!MockObject commentStamp: 'ZA 8/8/2006 22:10' prior: 0!
I am a mock object who can be instructed to answer specific messages with predefined mock answers.!


!MockObject methodsFor: 'initialize-release' stamp: 'ZA 8/8/2006 21:19'!
initialize
	super initialize.
	answers := Dictionary new! !


!MockObject methodsFor: 'mock' stamp: 'ZA 8/8/2006 22:14'!
answer: aBlock with: anObject
	self answerWithMockAnswer.
	(aBlock value) value: anObject.
	self answerWithMockAnswerValue.
! !

!MockObject methodsFor: 'mock' stamp: 'ZA 8/8/2006 21:32'!
answerWithMockAnswer.
	answerWithMockAnswer := true
! !

!MockObject methodsFor: 'mock' stamp: 'ZA 8/8/2006 21:33'!
answerWithMockAnswerValue
	answerWithMockAnswer := false
! !

!MockObject methodsFor: 'mock' stamp: 'ZA 8/8/2006 21:58'!
doesNotUnderstand: aMessage 
	^ answerWithMockAnswer
		ifTrue: [answers
				at: aMessage
				ifAbsentPut: [MockAnswer new]]
		ifFalse: [(answers includesKey: aMessage)
				ifTrue: [(answers at: aMessage) value]
				ifFalse: [super doesNotUnderstand: aMessage]]! !
-------------- next part --------------
'From Squeak3.9alpha of 4 July 2005 [latest update: #7048] on 8 August 2006 at 10:23:12 pm'!

!Message methodsFor: '*mock' stamp: 'ZA 8/8/2006 21:43'!
= anObject 
	^ (anObject isKindOf: self class)
		and: [selector = anObject selector & (args = anObject arguments) & (lookupClass = anObject lookupClass)]! !
-------------- next part --------------
'From Squeak3.9alpha of 4 July 2005 [latest update: #7048] on 8 August 2006 at 10:23:37 pm'!

!Message methodsFor: '*mock' stamp: 'ZA 8/8/2006 21:49'!
hash
	^ selector hash! !
-------------- next part --------------
'From Squeak3.9alpha of 4 July 2005 [latest update: #7048] on 8 August 2006 at 10:22:59 pm'!
Object subclass: #MockAnswer
	instanceVariableNames: 'value'
	classVariableNames: ''
	poolDictionaries: ''
	category: 'Mock-Prototype'!
!MockAnswer commentStamp: 'ZA 8/8/2006 22:11' prior: 0!
I am a mock answer whose value should be returned in response to a predefined message. I could be much more than a simple value holder if given the chance. I should only ever be dealt with by MockObject, don't let me out on my own, I'll only embarras you.!


!MockAnswer methodsFor: 'accessing' stamp: 'ZA 8/8/2006 21:09'!
value
	^ value! !

!MockAnswer methodsFor: 'accessing' stamp: 'ZA 8/8/2006 21:09'!
value: anObject
	value := anObject! !


!MockAnswer methodsFor: 'printing' stamp: 'ZA 8/8/2006 22:12'!
printOn: aWriteStream 
	^ value printOn: aWriteStream! !


More information about the Squeak-dev mailing list