DEV Community

Discussion on: Explain Unit Testing like I'm five

Collapse
 
cjbrooks12 profile image
Casey Brooks

Continuing with this analogy, I'll introduce mocking and its importance.

So you're pretty confident that the doll will not survive a drop onto your sister's floor, and you really want to test this. The problem is that your sister has friends over and her door is locked, so you can't actually go in there to test it. But you did already manage to steal the doll.

You know her room has wooden floors, but is usually covered in clothes. So you go to your room (which you normally keep very clean) and start throwing some clothes around. This way, you can test drop it in your room knowing that it would have the same effect as if you were to drop it in hers.

But this gets you thinking. Instead of throwing clothes on your floor, what would happen if you threw pillows around? Or dropped it onto carpet or tile instead. So you set up a series of situations where you drop the doll onto floors of these different types, but you do it in a very controlled manner so you know the only thing that changed from one test to the next is the floor.