views:

106

answers:

4

Hello,

I was just curious, afer seeing an ad on S/O, and clicking on it, then downloading it from Google Code...

Why would anybody want/need to create a Fake Object? I don't object to it, or don't think there's anything wrong with it, I just really don't know.

Thanks!

+1  A: 

Because sometimes it's too expensive, or otherwise undesirable to use a real object.

The most use is for unit testing frameworks, where you want to test a component in isolation, which means providing stubs for things that you don't want to test against.

Another common use is to stub out functionality at runtime without having to write code everywhere that tests whether the functionality is enabled or not. Logging is a fairly typical example.

Marcelo Cantos
+2  A: 

Are you referring to mock objects? If so, I'm not going to be able to improve upon the Wiki article:

"In object-oriented programming, mock objects are simulated objects that mimic the behavior of real objects in controlled ways. A computer programmer typically creates a mock object to test the behavior of some other object, in much the same way that a car designer uses a crash test dummy to simulate the dynamic behavior of a human in vehicle impacts. In a unit test, mock objects can simulate the behavior of complex, real (non-mock) objects and are therefore useful when a real object is impractical or impossible to incorporate into a unit test."

Kirk Woll
+1  A: 

I often made some "Null Objects" as I call them that have the interface of some other object and just do nothing. Maybe that's waht you're talking about. This objects are very usefull when you don't want to ask every time for an object to be null.

unkiwii
+1  A: 

Here is an example:

Assume that you have to test whether your a method called SendEmail() is being called in your EmailService Class. You can test this by using the real object, but your SendEmail() method might actually contain the production code to send emails. You might not want to do this while testing, so instead, you can test to see whether SendEmail() is being called by mocking or faking the object. This way you know SendEmail() is called when it should be without having to actually send out an e-mail. To take it a bit further, assume that you have to test whether SendEmail() only sends out e-mails at a specific time interval, like between 6 and 8p.m. How are you going to test this? You can change your system clock, but why do this when you can fake the current DateTime for the EmailService in order to test SendEmail()

Xaisoft
Ah, I understand now. That's really cool, thanks! Now my friend won't get heaps of random [TEST] emails lol.
lucifer