A. That get_user_data returns user name 'Alice' using mocked DB
B. That the real database returns user Alice
C. That the database call is skipped entirely
D. That the service raises an error for user 1
Solution
Step 1: Analyze mocking effect
The database call get_user is replaced by a mock returning fixed user data with name 'Alice'.
Step 2: Understand test assertion
The test checks if get_user_data returns a result with name 'Alice', confirming it uses the mocked data.
Final Answer:
That get_user_data returns user name 'Alice' using mocked DB -> Option A
Quick Check:
Mocked DB returns Alice, test checks service uses it [OK]
Hint: Mock return_value sets test data; assert checks service output [OK]
Common Mistakes:
Assuming real DB is called
Thinking database call is skipped without replacement
Expecting error instead of valid data
4. A developer writes this unit test for a microservice method:
def test_process_order():
result = process_order(123)
assert result == 'Success'
But the test fails because process_order calls an external payment service. What is the best fix?
medium
A. Rewrite process_order to not call payment service
B. Add a mock for the external payment service call
C. Run the test only when payment service is available
D. Remove the assertion to avoid failure
Solution
Step 1: Identify external dependency issue
process_order calls an external service, causing test failure due to dependency.
Step 2: Apply mocking to isolate test
Mocking the external payment service call isolates the unit test and avoids real external calls.
Final Answer:
Add a mock for the external payment service call -> Option B
Quick Check:
Mock external calls to isolate unit tests [OK]
Hint: Mock external services to avoid test failures [OK]
Common Mistakes:
Removing assertions instead of fixing dependencies
Running tests only when external services are up
Changing production code to fix tests
5. You want to unit test a microservice method that calls two other services: a user service and an inventory service. Which approach best ensures your unit test is reliable and fast?
hard
A. Skip testing this method because it depends on other services
B. Call both real services during the test to check integration
C. Mock both user and inventory service calls with fixed responses
D. Test only the user service call and ignore inventory service
Solution
Step 1: Understand unit test isolation
Unit tests should isolate the method by mocking external service calls to avoid flakiness and slowness.
Step 2: Apply mocks to all external dependencies
Mocking both user and inventory service calls ensures the test is reliable and fast without real network calls.
Final Answer:
Mock both user and inventory service calls with fixed responses -> Option C
Quick Check:
Mock all external calls for reliable, fast unit tests [OK]
Hint: Mock all external services for isolated unit tests [OK]
Common Mistakes:
Calling real services in unit tests
Skipping tests due to dependencies
Partially mocking dependencies leading to flaky tests