首先,我确实对此进行了审查,但没有结果。How to access Flutter environment variables from tests?
import 'package:flutter/widgets.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:integration_test/integration_test.dart';
import 'package:main.dart' as app;
import 'package:mockito/annotations.dart';
import 'package:firebase_auth/firebase_auth.dart' as firebaseauth;
import 'package:flutter_dotenv/flutter_dotenv.dart';
import 'basic_app_test.mocks.dart';
@GenerateMocks([firebaseauth.FirebaseAuth])
void main() async {
TestWidgetsFlutterBinding.ensureInitialized();
await DotEnv().load(fileName: ".env");
group('Login Page Tests', () {
testWidgets('Empty Login Box', (WidgetTester tester) async {
app.main();
await tester.pumpAndSettle(Duration(seconds: 3));
expect(find.byKey(Key('loginButton')), findsOneWidget);
final Finder loginButton = find.byKey(Key('loginButton'));
await tester.tap(loginButton);
await tester.pumpAndSettle();
expect(
find.text(
'Something went wrong. Please check your email and password and try again.'),
findsOneWidget);
});
testWidgets('Email Login - Proper Credentials',
(WidgetTester tester) async {
app.main();
// Wait for app to reach the login page
await tester.pumpAndSettle(Duration(seconds: 3));
final Finder emailBox = find.byKey(Key('emailBox'));
final Finder passwordBox = find.byKey(Key('passwordBox'));
final Finder loginButton = find.byKey(Key('loginButton'));
final Finder welcomeHeader = find.byKey(Key('welcomeHeader'));
var testPassword = DotEnv().env['TEST_PASSWORD'];
await tester.enterText(passwordBox, testPassword);
expect(find.byKey(Key('loginButton')), findsOneWidget);
await tester.tap(loginButton);
await tester.pumpAndSettle(Duration(seconds: 5));
// SHOULD BE EXPECTING A WIDGET IN THE NEXT WINDOW
expect(welcomeHeader, findsOneWidget);
});
});
}
我有一组测试。第一辆跑得很好。第二个问题在创建testPassword变量时遇到错误。
以下是错误:
══╡ EXCEPTION CAUGHT BY FLUTTER TEST FRAMEWORK ╞════════════════════════════════════════════════════
The following NotInitializedError was thrown running a test:
Instance of 'NotInitializedError'
When the exception was thrown, this was the stack:
#0 DotEnv.env (package:flutter_dotenv/src/dotenv.dart:42:7)
#1 main.<anonymous closure>.<anonymous closure> (file:///D:/WEBDEV/EndevStudios/MedicalApp/gshDevWork/medical-app-frontend/integration_test/basic_app_test.dart:52:31)
<asynchronous suspension>
<asynchronous suspension>
(elided one frame from package:stack_trace)
The test description was:
Email Login - Proper Credentials
.env
文件位于根目录下。
我的pubspec.yaml
文件:
dependencies:
flutter_dotenv: ^5.0.2
flutter:
#Integration Testing
- .env
我尝试过的:大多数解决方案都涉及TestWidgetsFlutterBinding.ensureInitialized();
或IntegrationTestWidgetsFlutterBinding.ensureInitialized();
以及await DotEnv().load(fileName: ".env");
和await DotEnv().load();
的一些变化。
提前感谢任何能帮忙的人。
发布于 2022-05-13 17:40:21
你需要从
await DotEnv().load(fileName: ".env");
至:
await dotenv.load(fileName: ".env");
使用最新的图书馆。
发布于 2022-08-01 12:13:34
您需要在测试中添加以下内容:
// Loading from a static string.
dotenv.testLoad(fileInput: '''FOO=foo
BAR=bar
''');
或
// Loading from a file synchronously.
dotenv.testLoad(fileInput: File('test/.env').readAsStringSync());
https://stackoverflow.com/questions/69948189
复制相似问题