satisfyAllConditions will show all errors at once, not only first fail assertion.
test('should satisfy all conditions', () {
Should.satisfyAllConditions([
() => playerCharacter.nickname.should.as('nickname').not.beNullOrEmpty(),
() => playerCharacter.weapons.should.as('weapons').contain('Staff of Wonder'),
() => playerCharacter.health.should.as('health').be(100),
]);
});
Example report can be:
Expected satisfy all conditions specified, but does not.
The following errors were found ...
------------- Error 1 -------------
Expected nickname
should not be null or empty
but was
''
------------- Error 2 -------------
Expected weapons
should contain
Staff of Wonder
but was actually
[Axe, Sword]
------------- Error 3 -------------
Expected health
should be
100
but was
99
------------------------------------
Custom matchers
For existing one - just create an extension for it
extensionCustomNumAssertionsonNumericAssertions {
NumericAssertionsget beNegative {
if (subject >= 0) {
throwShouldlyTestFailureError('Number\n should be negative');
}
returnNumericAssertions(subject);
}
}
Or more exotic matchers - fot custom types
First, create a Assertions class for your custom type which extends BaseAssertions
classCustomerAssertionsextendsBaseAssertions<Customer, CustomerAssertions> {
// ...CustomerAssertionsget beMarried {
if (isReversed) {
if (subject!.isMarried) {
throwShouldlyTestFailureError('Customer should not be married');
}
} else {
if (!subject!.isMarried) {
throwShouldlyTestFailureError('Customer should be married');
}
}
returnCustomerAssertions(subject);
}
CustomerAssertionsget beMale {
if (isReversed) {
Execute.assertion
.forCondition(subject!.gender == Gender.male)
.failWith('$subjectLabel should not be male');
} else {
Execute.assertion
.forCondition(subject!.gender != Gender.male)
.failWith('$subjectLabel should be male');
}
Execute.assertion
.forCondition(subject!.gender != Gender.male)
.failWith('$subjectLabel should be male');
returnCustomerAssertions(subject);
}
// ...
}
Second, create an extension on your class which has should property with type of your custom assertions class
extensionCustomerExtensiononCustomer {
CustomerAssertionsget should => CustomerAssertions(this);
}
Third, you are ready to go use your own shouldly assertions
test('async function should throw exception', () async {
awaitShould.throwAsync(() {
Future.delayed(Duration(milliseconds: 100));
throwException('test');
});
});
test('async function should throw exact exception', () async {
awaitShould.throwAsync<CustomException>(() {
Future.delayed(Duration(milliseconds: 100));
throwCustomException('custom exception test');
});
});
test('should complete in a duration', () async {
awaitShould.completeIn(
Duration(seconds: 1),
func: () => slowFunction(
Duration(milliseconds: 900),
),
);
});
Enums
test('should not be equal', () {
seasons.spring.should.not.be(seasons.winter);
});
test('should not be type of', () {
seasons.spring.should.not.beOfType<level>();
});
test('should be assignable to `Enum`', () {
seasons.spring.should.beAssignableTo<Enum>();
});
extensionCustomerExtensiononCustomer {
CustomerAssertionsget should => CustomerAssertions(this);
}
classCustomerAssertionsextendsBaseAssertions<Customer, CustomerAssertions> {
CustomerAssertions(
Customer? subject, {
bool isReversed = false,
String? subjectLabel,
}) : super(subject, isReversed: isReversed, subjectLabel: subjectLabel);
CustomerAssertionsget beMarried {
if (isReversed) {
if (subject!.isMarried) {
throwShouldlyTestFailure('Customer should not be married');
}
} else {
if (!subject!.isMarried) {
throwShouldlyTestFailure('Customer should be married');
}
}
returnCustomerAssertions(subject);
}
CustomerAssertionsget beMale {
if (isReversed) {
if (subject!.gender == Gender.male) {
throwShouldlyTestFailure('Customer should be female');
}
} else {
if (subject!.gender != Gender.male) {
throwShouldlyTestFailure('Customer should be male');
}
}
returnCustomerAssertions(subject);
}
@overrideCustomerAssertionscopy(
Customer? subject, {
bool isReversed = false,
String? subjectLabel,
}) =>
CustomerAssertions(
subject,
isReversed: isReversed,
subjectLabel: subjectLabel,
);
}
Recommendations
You can improve the readability of the rest of your test code with given_when_then_unit_test, which enhances the test report readability as well.
Changelog
Please see the Changelog page to know what's recently changed.
Contributing
Feel free to contribute to this project.
If you find a bug or want a feature, but don't know how to fix/implement it, please fill an issue.
If you fixed a bug or implemented a new feature, please send a pull request.