Lens

Mocks

Unit testing works well for methods that have controlled dependencies and no side effects.
Let’s write a class that depends on an uncontrolled external resource and has dangerous side effects.
Here’s an example:

This “Terminal” class communicates with the user by sending text messages through the terminal.
When the user is typing in a response, this code will block the execution of the script.
That blocking behavior will make it a real challenge to run our tests!

But first, let’s see it in action.
Let’s write a tiny class that uses the “Terminal”: