Test doubles are used in unit testing to simulate components. Which usage is correct?

Enhance your coding skills with the Code Standards and Practices Level 3 Test. Access well-crafted questions, insightful explanations, and progress tracking to master this exam. Prepare effectively for your Level 3 certification with our comprehensive study materials!

Multiple Choice

Test doubles are used in unit testing to simulate components. Which usage is correct?

Explanation:
Understanding how test doubles are used helps you see why this mapping fits the question’s framing. In this view, a mock is the tool used to produce fixed outputs during the test, a stub is the one you use to verify that certain interactions occurred (ensuring the code talks to its collaborators as expected), and a fake is a lightweight substitute that introduces a different, simpler implementation of a component (often one that behaves like a real system but with fewer side effects, such as connecting to an external system). This separation—controlling return values, asserting interactions, and swapping in a simple substitute—keeps the unit test focused and deterministic, which is the core goal of using test doubles. The other options mix up these roles or describe fakes in ways that don’t align with how the question intends the terms to be used, so they’re less consistent with the scenario given.

Understanding how test doubles are used helps you see why this mapping fits the question’s framing. In this view, a mock is the tool used to produce fixed outputs during the test, a stub is the one you use to verify that certain interactions occurred (ensuring the code talks to its collaborators as expected), and a fake is a lightweight substitute that introduces a different, simpler implementation of a component (often one that behaves like a real system but with fewer side effects, such as connecting to an external system). This separation—controlling return values, asserting interactions, and swapping in a simple substitute—keeps the unit test focused and deterministic, which is the core goal of using test doubles.

The other options mix up these roles or describe fakes in ways that don’t align with how the question intends the terms to be used, so they’re less consistent with the scenario given.

Subscribe

Get the latest from Passetra

You can unsubscribe at any time. Read our privacy policy