42 lines
1.9 KiB
Dart
42 lines
1.9 KiB
Dart
// 冒烟测试:验证 App 根组件能正常构建 + 登录守卫行为
|
||
//
|
||
// 为什么不是直接断言 4 个 tab 文案:
|
||
// TabApp 的 go_router 带登录守卫(app_router.dart _guardLogin),
|
||
// 测试进程里没有 token → 访问 / 被 redirect 到 /login,
|
||
// 所以首帧渲染的是登录页而不是主框架。
|
||
//
|
||
// 测试要点:
|
||
// 1. app.dart 实际在 lib/core/ 下(旧路径 package:new_learn/app.dart 是错的);
|
||
// 2. TabApp 内部有 ConsumerWidget(登录页用了 ref),必须包 ProviderScope;
|
||
// 3. 登录页 initState 异步读"记住的账号/密码",依赖两个本地存储插件,
|
||
// 测试环境没有原生插件,必须先 mock(否则抛 MissingPluginException):
|
||
// SharedPreferences.setMockInitialValues(shared_preferences 官方)
|
||
// FlutterSecureStorage.setMockInitialValues(flutter_secure_storage 官方)
|
||
|
||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
|
||
import 'package:flutter_test/flutter_test.dart';
|
||
import 'package:shared_preferences/shared_preferences.dart';
|
||
|
||
import 'package:new_learn/core/app.dart';
|
||
|
||
void main() {
|
||
setUp(() {
|
||
// 登录页 initState 会异步读这两个插件,先给空的 mock 数据
|
||
SharedPreferences.setMockInitialValues({});
|
||
FlutterSecureStorage.setMockInitialValues({});
|
||
});
|
||
|
||
testWidgets('未登录守卫冒烟:TabApp 应重定向到登录页', (WidgetTester tester) async {
|
||
// 构建根组件并触发一帧,确认没有抛异常
|
||
await tester.pumpWidget(const ProviderScope(child: TabApp()));
|
||
await tester.pumpAndSettle();
|
||
|
||
// 守卫生效:无 token 访问 / → 被踢到 /login
|
||
expect(find.text('立即登录'), findsOneWidget);
|
||
expect(find.text('爱学蝶变'), findsOneWidget);
|
||
// 未登录时不应看到主框架的 tab 文案
|
||
expect(find.text('任务'), findsNothing);
|
||
});
|
||
}
|