|
1 | 1 | // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
2 | 2 | // SPDX-License-Identifier: Apache-2.0
|
3 | 3 |
|
| 4 | +import 'dart:async'; |
| 5 | + |
4 | 6 | import 'package:amplify_analytics_pinpoint/amplify_analytics_pinpoint.dart';
|
5 | 7 | import 'package:amplify_analytics_pinpoint_example/amplifyconfiguration.dart';
|
6 | 8 | import 'package:amplify_api/amplify_api.dart';
|
@@ -34,11 +36,40 @@ void main() {
|
34 | 36 | ),
|
35 | 37 | AmplifyAPI(),
|
36 | 38 | ]);
|
37 |
| - await expectLater( |
38 |
| - Amplify.configure(amplifyEnvironments[environmentName]!), |
39 |
| - completes, |
| 39 | + await completesWithoutError( |
| 40 | + () => Amplify.configure(amplifyEnvironments[environmentName]!), |
40 | 41 | );
|
41 | 42 | });
|
42 | 43 | }
|
43 | 44 | });
|
44 | 45 | }
|
| 46 | + |
| 47 | +/// Tests that [callback] can complete without an error, while ignoring any errors |
| 48 | +/// that are not the direct result of [callback]. |
| 49 | +/// |
| 50 | +/// This is useful for testing methods that may initiate other async functions |
| 51 | +/// without waiting for them to complete. If async functions that are not awaited |
| 52 | +/// on result in an error, they are ignored. |
| 53 | +/// |
| 54 | +/// Once analytics is configured, events will be sent to pinpoint every n seconds. |
| 55 | +/// If the user is not signed in and guest access is not enabled, these requests |
| 56 | +/// will fail until the user logs in. These failures are expected and should not |
| 57 | +/// fail the test. |
| 58 | +Future<void> completesWithoutError(Future<Object?> Function() callback) async { |
| 59 | + await runZonedGuarded( |
| 60 | + () async { |
| 61 | + await callback().onError( |
| 62 | + (error, stackTrace) { |
| 63 | + fail('should complete without error but failed with $error'); |
| 64 | + }, |
| 65 | + ); |
| 66 | + }, |
| 67 | + (error, stack) { |
| 68 | + if (error is TestFailure) { |
| 69 | + fail(error.message ?? 'should complete without error'); |
| 70 | + } else { |
| 71 | + // ignore errors that do not arise from the provided callback |
| 72 | + } |
| 73 | + }, |
| 74 | + ); |
| 75 | +} |
0 commit comments