expect method
Send request and assert the response has a specific status code.
Convenience method that sends the request and automatically validates the response status code. Throws a TestFailure if the status doesn't match.
Example:
// Expect successful creation
final response = await testApp.post('/api/users')
.json({'name': 'John'})
.expect(201);
// Expect not found
await testApp.get('/api/users/999').expect(404);
statusCode Expected HTTP status code.
Returns the TestResponse if status matches.
Throws TestFailure if status code doesn't match expectation.
Implementation
Future<TestResponse> expect(int statusCode) async {
final response = await send();
if (response.statusCode != statusCode) {
throw TestFailure(
'Expected status $statusCode but got ${response.statusCode}\n'
'Response body: ${response.body}',
);
}
return response;
}