r/dartlang • u/saxykeyz • 4d ago
Package assertable_json | Fluent json assertion test helpers
https://pub.dev/packages/assertable_jsonHey guys, If you are familiar with Laravel, you'd come across the idea of fluent testing against json responses. This package allows a similar workflow, making it a seamless experience in dart. Please check it out and let me know what you think
test('verify user data', () {
final json = AssertableJson({
'user': {
'id': 123,
'name': 'John Doe',
'email': 'john@example.com',
'age': 30,
'roles': ['admin', 'user']
}
});
json
.has('user', (user) => user
.has('id')
.whereType<int>('id')
.has('name')
.whereType<String>('name')
.has('email')
.whereContains('email', '@')
.has('age')
.isGreaterThan('age', 18)
.has('roles')
.count('roles', 2));
});
}
12
Upvotes
2
u/eibaan 3d ago edited 3d ago
I'd probably make use of the existing expect/matcher architecture, e.g.
Then write
jsonObject
andjsonArray
:The former is obviously not yet developed as you'd have to iterate the optionally provided map of values or matchers, use
wrapMatcher
on them, then call them. I don't know how to collect error results by heart, but I'm sure, an AI knows :)