2

Is there a way to test for building a widget throwing an exception.

I tried this in testWidgets but it doesn't recognize the error.

expectLater(() async {
  await tester.pumpWidget(...);
}, throwsException);
1
  • You probably want to throw on the constructor instead of build. Commented Nov 12, 2018 at 7:13

1 Answer 1

2

You can only catch exceptions thrown by code invoked by your code.
build() is invoked by the Flutter framework and therefore errors land there.

You can register a custom error handler for such exceptions in your test

final errorHandled = expectAsync0((){});

FlutterError.onError = (errorDetails) {
  // handle error
  errorHandled();
});

This way the test will fail if errorHandled isn't called before the test times out.

https://docs.flutter.io/flutter/foundation/FlutterError-class.html

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.