Mock Objects for Testing
Mocking, or using “mock objects”, is essential to unit testing. I’ve seen it applied at least three times in the last two weeks. It’s usefulness can be distilled to the fact that it gives you tight control over the code path. With a mock object you can force your testing code to be right, but more importantly, to be wrong. Mock objects also give you the ability to circumvent dependencies and waits. For example, my mocking Swift’s
URLSession you can write unit tests where you can assume the networking code executes successfully — this lets you focus on the code you care about testing.
Here are some valuable introductions on the topic: