Testing Code for Idempotency
There’s no general way to test a piece of code to determine if it’s idempotent. It’s usually not possible to have all of your third-party service providers simulate error conditions, so testing idempotent code often involves testing that it behaves the way you intended, given the conditions you wrote it for.
The changes to send_notification_email were substantial. You added a new API call and complex logic to examine that call to figure out what to do. The example app doesn’t use mocking but since it does have the fake mail service running, you can use it to simulate the situation you’re checking for.
The core of the test will call send_notification_email, and will expect a previously-used email_id to be set ...
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