我正在尝试使用 Flutter 使用 Anroid Studio 创建一个应用程序,该应用程序使用 Firebase 作为实时数据库。我想创建一些测试来检查下载/发送到数据库的数据是否正确检索/发送。我在与数据库建立连接时遇到问题。我尝试更新依赖项,使用命令“flutter pub升级firebase_core”,“flutter pub升级firebase_messaging”,“flutter clean”,“flutter pub outdated”,“flutter pub升级outdated_package”。
每次使用命令 flutter test 时,我都会收到以下错误(缩短以更具可读性):
00:06 +0: app_test.dart: (setUpAll) Failed to initialize Firebase: PlatformException(channel-error, Unable to establish connection on channel.) package:firebase_core_platform_interface/src/pigeon/messages.pigeon.dart 210:7 FirebaseCoreHostApi.initializeCore To run this test again: dart.exe test app_test.dart -p vm --plain-name "(setUpAll)" 00:06 +0 -3: app_test.dart: 'INTEGRATION TEST - ADD MARKER ON MAP' [E] Assertion failed: '_pendingFrame == null' To run this test again: dart.exe test app_test.dart -p vm --plain-name “INTEGRATION TEST - ADD MARKER ON MAP'" 00:06 +0 -4: app_test.dart: (tearDownAll) Warning: integration_test plugin was not detected. If running with flutter drive, use flutter test $path_to_test instead.
生成此输出的代码主要分为两个 dart 文件,其他文件共享非常相似的代码用于其他类型的测试。
第一个 dart 文件是 app_test.dart:
import package:cobol_lab/firebase_options.dart;
import package:flutter/material.dart;
import package:flutter_test/flutter_test.dart;
import package:google_maps_flutter/google_maps_flutter.dart;
import package:integration_test/integration_test.dart;
import package:firebase_core/firebase_core.dart;
import package:cobol_lab/main.dart as app;
void main() {
IntegrationTestWidgetsFlutterBinding.ensureInitialized();
setUpAll(() async {
await Firebase.initializeApp(
options: DefaultFirebaseOptions.currentPlatform,
);
});
testWidgets(INTEGRATION TEST - MAP OPENING, (WidgetTester tester) async {
app.main();
await tester.pumpAndSettle();
// CHECK IF MAP IS LOADED
expect(find.byType(GoogleMap), findsOneWidget);
});
testWidgets(INTEGRATION TEST - ADD MARKER ON MAP, (WidgetTester tester) async {
app.main();
await tester.pumpAndSettle();
// SIMULATION OF CLICK OF BUTTON TO INSERT MARKER
await tester.tap(find.byIcon(Icons.add_location));
await tester.pumpAndSettle();
expect(find.text(INSERT NEW ADDRESS), findsOneWidget);
});
}
第二个文件是my_map_test.dart:
import package:cobol_lab/firebase_options.dart;
import package:firebase_core/firebase_core.dart;
import package:flutter_test/flutter_test.dart;
import package:cobol_lab/mappa.dart;
void main() {
setUpAll(() async {
TestWidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp(
options: DefaultFirebaseOptions.currentPlatform,
);
});
test(Test inizializzazione MyMap, () {
final myMap = MyMap();
expect(myMap, isNotNull);
});
}
在文件 pubspec.yaml 中,我有以下依赖项:
sdk: fluttercupertino_icons: ^1.0.6
google_maps_flutter: ^2.6.0
google_maps_flutter_web: ^0.5.6+2
location: ^5.0.3
fluttertoast: ^8.0.8
geocoding: ^2.0.0
firebase_core: ^2.28.0
firebase_auth: ^4.18.0
firebase_database: 10.4.11
dev_dependencies:
flutter_test:
sdk: flutter
integration_test:
sdk: flutter
要使测试被视为集成测试,测试文件必须位于
integration_test
目录而不是 test
目录中。
/test
/integration_test
如果还没有,您可以移动文件并连接设备或模拟器。
连接设备或模拟器后,您可以使用
flutter test integration_test
命令运行集成测试。如果您的项目有风格,您可以使用 flutter test integration_test—-flavor your--flavor
命令来运行具有特定风格的测试。