Numeric Testing
server_testing provides comprehensive numeric testing capabilities through extensions and assertions on AssertableJson.
Basic Numeric Assertions
response.assertJson((json) {
json
// Basic comparisons
.isGreaterThan('age', 18)
.isLessThan('price', 100)
.isGreaterOrEqual('quantity', 1)
.isLessOrEqual('discount', 50)
// Range testing
.isBetween('score', 0, 100)
// Mathematical checks
.isDivisibleBy('quantity', 5)
.isMultipleOf('price', 10)
.isPerfectSquare('area') // e.g., 16, 25, 36
// Sign testing
.isPositive('balance')
.isNegative('debt')
.isZero('count')
// Number properties
.isEven('evenNumber')
.isOdd('oddNumber')
.isPrime('primeNumber') // Tests if number is prime
});
Mathematical Functions
response.assertJson((json) {
json
// Square root
.where('sqrt', (value) => value.sqrt() == 5) // value = 25
// Power
.where('squared', (value) => value.pow(2) == 25) // value = 5
// Custom calculations
.where('total', (value) {
final number = value as num;
return number.isGreaterThan(100) &&
number.isDivisibleBy(10);
});
});
Array Operations
response.assertJson((json) {
json
// Array length
.count('items', 5)
.countBetween('items', 1, 10)
// Array items
.scope('numbers', (numbers) {
numbers.each((num) {
num
.isGreaterThan(0)
.isLessThan(100)
.isDivisibleBy(5);
});
});
});
Numeric Validation in Schema
response.assertJson((json) {
json.matchesSchema({
'id': int,
'score': double,
'metrics': {
'count': int,
'average': double,
'values': List<num>
}
});
});
Error Margins
response.assertJson((json) {
json
// Approximate equality
.where('pi', (value) => (value - 3.14159).abs() < 0.00001)
// Range checks with tolerance
.where('temperature', (value) {
final temp = value as num;
return temp.isBetween(36.5, 37.5);
});
});
Conditional Testing
response.assertJson((json) {
json
// Test conditionally
.when(isPremiumUser, (json) {
json
.isGreaterThan('credits', 1000)
.isPositive('bonus');
})
// Test numeric ranges
.where('score', (value) {
final score = value as num;
if (isDifficultMode) {
return score.isBetween(80, 100);
} else {
return score.isBetween(60, 100);
}
});
});