Chapter 20. Using Mocks to Test External Dependencies
In this chapter, we’ll start testing the parts of our code that send emails—i.e., the second item on our scratchpad:
In the functional test (FT), you saw that Django gives us a way of retrieving
any emails it sends by using the mail.outbox attribute.
But in this chapter, I want to demonstrate a widespread testing technique called mocking. So, for the purpose of these unit tests, we’ll pretend that this nice Django shortcut doesn’t exist.
Am I telling you not to use Django’s mail.outbox?
No—use it; it’s a neat helper.
But I want to teach you about mocks because they’re a useful general-purpose tool
for unit testing external dependencies.
You may not always be using Django!
And even if you are, you may not be sending email—any
interaction with a third-party API
is a place you might find yourself wanting to test with mocks.
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access