The test client sends a GET request to '/items', which is not allowed, causing a 405 error.
Final Answer:
Using GET method instead of POST for /items -> Option B
Quick Check:
Method mismatch causes error = Using GET method instead of POST for /items [OK]
Hint: Match test request method with route method [OK]
Common Mistakes:
Using wrong HTTP method in test
Thinking function name affects routing
Misunderstanding import requirements
Creating TestClient after request
5. You want to test a FastAPI path operation that returns a list of users. Which approach correctly tests that the response contains exactly 3 users with names 'Alice', 'Bob', and 'Carol'?
hard
A. Check only the response status code is 200 without inspecting data
B. Send POST request with user data and check status code 201
C. Call the path operation function directly without TestClient
D. Use TestClient to GET the route, then assert response.json() == [{'name': 'Alice'}, {'name': 'Bob'}, {'name': 'Carol'}]
Solution
Step 1: Understand the goal of testing response data
We want to confirm the API returns exactly the expected list of users.
Step 2: Choose the correct testing method
Using TestClient to send a GET request and asserting the JSON response matches the expected list is correct.
Final Answer:
Use TestClient to GET the route, then assert response.json() == [{'name': 'Alice'}, {'name': 'Bob'}, {'name': 'Carol'}] -> Option D
Quick Check:
Test response data equality = Use TestClient to GET the route, then assert response.json() == [{'name': 'Alice'}, {'name': 'Bob'}, {'name': 'Carol'}] [OK]
Hint: Assert response.json() matches expected list exactly [OK]
Common Mistakes:
Testing with POST instead of GET for retrieval
Calling path function directly misses request context
Ignoring response data content in tests
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.