expectJson method

Future<TestResponse> expectJson(
  1. Map<String, dynamic> expected
)

Send request and assert the response contains expected JSON.

Convenience method that sends the request, parses the response as JSON, and validates it matches the expected structure using deep equality.

Example:

// Expect exact JSON match
await testApp.get('/api/users/123')
  .expectJson({
    'id': 123,
    'name': 'John Doe',
    'email': 'john@example.com'
  });

expected Expected JSON structure as a Map. Returns the TestResponse if JSON matches. Throws TestFailure if JSON doesn't match or isn't valid JSON.

Implementation

Future<TestResponse> expectJson(Map<String, dynamic> expected) async {
  final response = await send();

  try {
    final actual = jsonDecode(response.body) as Map<String, dynamic>;
    if (!_deepEquals(actual, expected)) {
      throw TestFailure(
        'JSON mismatch:\nExpected: $expected\nActual: $actual',
      );
    }
  } catch (e) {
    if (e is TestFailure) rethrow;
    throw TestFailure('Failed to parse response as JSON: $e');
  }

  return response;
}