Mock Objects

Chad Nantais chad at clearwaterassociates.ca
Tue Jun 27 15:33:12 UTC 2006


Stef,

I have attached the MockObject code and a test example.

Let me know if you are looking for something else.

Chad

On 6/26/06, stéphane ducasse <ducasse at iam.unibe.ch> wrote:
> Chad
>
> You know what would be great?
> If you could write by pasting some of your code in a text editor and
> share that with us.
> I would like to add some material to my Sunit tutorial and I could
> use your example.
>
> Stef
>
> On 23 juin 06, at 15:48, Chad Nantais wrote:
>
> > Frank & Jason,
> >
> > I don't need anything too deep right now.  I just need to be able to
> > create an object that mocks up a simple API for a remote service.
> >
> > I found this page and used it to create exactly what I needed.
> > http://www.cincomsmalltalk.com/userblogs/malby/SingleView.ssp?
> > showComments=true&forPrinter=true&entry=3293316866
> >
> > I'm also using it to test non-networked code now too.  For example,
> > when I am writing unit tests for Cart, and I want to test the
> > Cart>>total, which adds prices of CartItem instances that are in the
> > cart, I just mock up the CartItem instances and make them return some
> > number when sent #price.  This is great because I can really isolate
> > the Cart class that's being tested without it being dependant on
> > having a fully-working CartItem class.
> >
> > Thanks for your help.
> >
> > Chad
> >
> > On 6/23/06, Jason Rogers <jacaetevha at gmail.com> wrote:
> >> While this mock layer is good for many applications, I think it's
> >> probably too deep for what Chad is dealing with.  Chad -- you should
> >> be able to mock out the service itself and inject that into your
> >> application code.  While I haven't done this in a Smalltalk
> >> application I have successfully and easily done it in *many*
> >> applications in other languages.
> >>
> >> Jason
> >>
> >> On 6/23/06, Frank Shearar <frank.shearar at angband.za.org> wrote:
> >> > "Chad Nantais" <chad at clearwaterassociates.ca> wrote:
> >> >
> >> > > Hi.
> >> > >
> >> > > Could someone please direct me to a Squeak project (or some
> >> > > documentation) that uses mock objects in its tests, specifically
> >> > > dealing with testing network code like a web service.  I'm
> >> looking for
> >> > > a way to speed up some unit tests that hit a remote service.
> >> >
> >> > It's by no means fully functional, but you might want to look at
> >> > MockSocketStream. That SHOULD be in Squeak-3.8 and up, seeing as
> >> I wrote it
> >> > in 2004.
> >> >
> >> > Having a mock network layer not only lets you speed up tests, it
> >> also allows
> >> > you to disregard all the lovely things that can go wrong in the
> >> network. You
> >> > can run your tests knowing that you're not going to get red
> >> everywhere
> >> > because your colleague tripped over your fly lead, or is
> >> fiddling with a
> >> > patch panel in the next room. And, of course, a mock network
> >> layer lets you
> >> > INJECT errors :)
> >> >
> >> > I do intend adding to the MockSocketStream at some stage, but my
> >> plate's too
> >> > full for the next several months.
> >> >
> >> > frank
> >> >
> >> >
> >> >
> >>
> >>
> >> --
> >> Jason Rogers
> >>
> >> "Where there is no vision, the people perish..."
> >>     Proverbs 29:18
> >>
> >>
> >
>
>
>
-------------- next part --------------
"Example usage:
mock := MockObject new.
mock on: #selector do: [ aBlockToReturn ]."

Object subclass: #MockObject
	instanceVariableNames: 'callbacks'
	classVariableNames: ''
	poolDictionaries: ''
	category: 'MockObjects'
	
class>>new
	^super basicNew initialize

initialize
	callbacks := Dictionary new.

doesNotUnderstand: aMessage
	^(callbacks includesKey: aMessage selector)
		ifTrue: [ self performCallback: aMessage ]
		ifFalse: [ super doesNotUnderstand: aMessage ].

performCallback: aMessage
	^(callbacks at: aMessage selector)

		valueWithArguments: aMessage arguments

on: aSymbol do: aBlock
	callbacks at: aSymbol put: aBlock
-------------- next part --------------
TestCase subclass: #TestCartWithMultipleItems
	instanceVariableNames: 'cart item1 item2'
	classVariableNames: ''
	poolDictionaries: ''
	category: 'Cart-Tests'
	
setUp
	cart := Cart new.
	item1 := MockObject new.
	item1 on: #total do: [5.99].
	cart add: item1.
	item2 := MockObject new.
	item2 on: #total do: [2.99].

testShouldHaveATotalEqualToTheSumOfTotalsOfEachItem
	| sum |
	sum := 0.
	(cart items) do: [:i | sum := sum + i total].
	self assert: (sum = cart total)
	
testShouldAllowItemsToBeRemoved
	cart remove: item1.
	cart remove: item2.
	self assert: (cart isEmpty)	


More information about the Squeak-dev mailing list