What is a spy in testing?

Updated May 6, 2026

Short answer

A spy monitors function calls without modifying behavior.

Deep explanation

Spies track how functions are used (calls, arguments) while still executing original logic.

Real-world example

Verifying logging behavior in services.

Common mistakes

  • Confusing spies with mocks.

Follow-up questions

  • Spy vs mock difference?
  • When to use spies?

More Unit Testing interview questions

View all →