01.02.2013 Views

Software Development Cross Solution - Index of - Free

Software Development Cross Solution - Index of - Free

Software Development Cross Solution - Index of - Free

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

mock objects<br />

We need lots <strong>of</strong> different, but similar, objects<br />

The problem right now is that we have a sequence like this:<br />

304 Chapter 8<br />

processOrder(...)<br />

Order<br />

Processor<br />

getGC(...)<br />

saveGC(...)<br />

Test DB<br />

Utilities<br />

processOrder(...)<br />

saveOrder(...)<br />

What if we generated objects?<br />

return a specific<br />

GC with certain<br />

values<br />

do nothing...<br />

Instead <strong>of</strong> writing all these DBAccessor implementations,<br />

what if we had a tool—or a framework—that we could tell<br />

to create a new object, conforming to a certain interface (like<br />

DBAccessor), and that would behave in a certain way, like<br />

returning a gift card with a zero balance provided a certain<br />

input was passed in?<br />

Your test code can use this<br />

object like any other... it<br />

implements DBAccessor and<br />

looks just like a real class that<br />

you’d write yourself.<br />

fake-save the GC<br />

This is all fake, but since the<br />

order processor has to call<br />

these methods, we need an<br />

implementation <strong>of</strong> DBAccessor.<br />

Mock<br />

Object<br />

Download at WoweBook.Com<br />

Here’s where the problem is: for each<br />

different type <strong>of</strong> scenario, we need<br />

a different test class to return the<br />

right kind <strong>of</strong> gift card.<br />

Your testing code tells the<br />

framework what it needs.<br />

TestGoodDBAccessor<br />

+ getGC(gcId : int) :GiftCard<br />

+ saveGC(card : GiftCard) :void<br />

TestInsufficientDBAccessor<br />

+ getGC(gcId : int) :GiftCard<br />

+ saveGC(card : GiftCard) :void<br />

TestInvalidDBAccessor<br />

+ getGC(gcId : int) :GiftCard<br />

+ saveGC(card : GiftCard) :void<br />

I want a DBAccessor implementation that<br />

returns a GiftCard with a zero balance, please.<br />

Here’s an object... if you<br />

call getGC() with a value<br />

<strong>of</strong> “12345,” it will do just<br />

what you want.<br />

Most languages have a<br />

framework just like this—just<br />

Google “mock objects.”<br />

Mock Object<br />

Framework

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!