Junior — Middle
Do you use mock objects in your project for testing or development?
sobes.tech AI
Answer from AI
Yes, mock objects (mocks, stubs) are widely used for testing and development. They allow isolating the tested component by replacing dependencies with fictitious objects with predictable behavior.
For example, when testing a function that interacts with a database, a mock object can be used to simulate database responses so that tests are fast and do not depend on external systems.
In Python, the unittest.mock library is often used for this purpose:
from unittest.mock import Mock
db_mock = Mock()
db_mock.get_user.return_value = {'id': 1, 'name': 'Alice'}
# Now you can test a function that calls db_mock.get_user
Using mock objects helps create reliable, fast, and independent tests.