Skip to main content

Response Assertions

TestResponse provides a rich set of assertions for verifying responses.

Status Assertions

response
// Basic status check
.assertStatus(200)

// Common status codes
.assertStatus(HttpStatus.ok)
.assertStatus(HttpStatus.created)
.assertStatus(HttpStatus.notFound)
.assertStatus(HttpStatus.unauthorized);

Header Assertions

response
// Check header exists
.assertHasHeader('Content-Type')

// Check exact header value
.assertHeader('Content-Type', 'application/json')

// Check header contains value
.assertHeaderContains('Content-Type', 'json')

// Check multiple values
.assertHeaderContains('Allow', ['GET', 'POST']);

Body Assertions

response
// Check body is not empty
.assertBodyIsNotEmpty()

// Check exact body content
.assertBodyEquals('{"status":"ok"}')

// Check body contains substring
.assertBodyContains('success')

// Check multiple substrings
.assertBodyContains('success')
.assertBodyContains('data');

JSON Assertions

Basic JSON Checking

response
// Check JSON structure
.assertJson((json) {
json
.has('user')
.has('meta')
.missing('error');
})

// Check specific path
.assertJsonPath('user.name', 'John')

// Check JSON subset
.assertJsonContains({
'user': {'name': 'John'},
'status': 'active'
});

Advanced JSON Assertions

response.assertJson((json) {
json
// Type checking
.whereType<int>('id')
.whereType<String>('email')
.whereType<List>('roles')

// Value matching
.where('name', 'John')
.whereNot('status', 'inactive')
.whereIn('role', ['admin', 'user'])

// Numeric comparisons
.isGreaterThan('age', 18)
.isLessThan('price', 100)
.isBetween('count', 1, 10)

// Array/object checking
.count('items', 3)
.countBetween('items', 1, 5)
.hasAll(['id', 'name', 'email'])

// Check unhandled properties
.etc(); // Marks all remaining properties as checked
});

Nested JSON Assertions

response.assertJson((json) {
// Check nested objects
json.scope('user', (user) {
user
.where('name', 'John')
.where('email', 'john@example.com')
.scope('preferences', (prefs) {
prefs
.where('theme', 'dark')
.where('language', 'en');
});
});

// Check arrays
json.scope('items', (items) {
// Check first item
items.first((item) {
item.where('id', 1);
});

// Check each item
items.each((item) {
item
.has('id')
.has('name')
.whereType<int>('id');
});
});
});

Conditional Assertions

response.assertJson((json) {
json
// Assert when condition is true
.when(isAdmin, (json) {
json.has('adminPanel');
})

// Assert when condition is false
.unless(isBasicUser, (json) {
json.has('advancedFeatures');
});
});

File Upload Assertions

response
.assertJsonFilesContain('uploads', 'profile.jpg')
.assertJson((json) {
json.scope('uploads', (uploads) {
uploads.each((file) {
file
.has('filename')
.has('size')
.has('content_type');
});
});
});

Debugging

response
// Print response details
.dump()

// Continue assertions
.assertStatus(200)
.assertJson((json) {
json
// Tap into assertion chain for debugging
.tap((j) => print('JSON: ${j.toString()}'))
.has('data');
});