Scenario个
我是小部件测试和GoRouter的新手.我有一个非常简单的测试用例:
- 有两个屏幕-
LoginPage
作为第一个屏幕加载,和CheckoutPage
-
LoginPage
有一个按钮.一旦我点击它,我就会被带到CheckoutPage
号公路.
我正在使用GoRouter进行导航.
Code个
以下是main.dart年度MaterialApp
的设置方式:
MaterialApp.router(
debugShowCheckedModeBanner: false,
routerConfig: GoRouteInformation.router,
title: "Neon Student",
),
以下是routerConfig
个和RouteNames
个班级:
100
class GoRouteInformation {
static final GoRouter router = GoRouter(
routes: [
GoRoute(
path: '/',
name: RouteNames.login,
builder: (context, state) => const LoginPage(),
routes: [
GoRoute(
path: 'checkout',
name: RouteNames.checkout,
builder: (context, state) => const CheckoutPage(),
),
],
),
],
);
}
100
class RouteNames {
static const String login = 'login';
static const String checkout = 'checkout';
}
以下是loginpage_test.dart中的代码:
testWidgets('Navigates from LoginPage to CheckoutPage', (WidgetTester tester) async {
await tester.pumpWidget(const MainApp());
await tester.pumpWidget(const MaterialApp(home: LoginPage()));
expect(find.byType(LoginSection), findsOneWidget);
expect(find.byType(SplitDivider), findsOneWidget);
Finder subscribeButton = find.byKey(const ValueKey('Subscribe'));
expect(subscribeButton, findsOneWidget);
await tester.tap(subscribeButton);
await tester.pumpAndSettle();
expect(find.byType(CheckoutPage), findsOneWidget);
});
EDIT:这是我的main.dart
void main() {
runApp(const MainApp());
}
class MainApp extends StatelessWidget {
const MainApp({super.key});
@override
Widget build(BuildContext context) {
return ScreenUtilInit(
designSize: const Size(428, 926),
minTextAdapt: true,
splitScreenMode: true,
builder: (_, child) {
return GestureDetector(
onTap: () {
FocusManager.instance.primaryFocus?.unfocus();
},
behavior: HitTestBehavior.opaque,
child: MaterialApp.router(
debugShowCheckedModeBanner: false,
routerConfig: GoRouteInformation.router,
title: "Neon Student",
),
);
},
);
}
}
Problem个
当我运行flutter test时,我得到以下错误:
- The following assertion was thrown while handling a gesture: No GoRouter found in context 'package:go_router/src/router.dart': Failed assertion: line 507 pos 12: 'inherited != null'个
- The following TestFailure was thrown running a test: Expected: exactly one matching candidate Actual: _TypeWidgetFinder:<Found 0 widgets with type "CheckoutPage": []> Which: means none were found but one was expected个
Request个
我想知道我的小部件测试代码中有什么错误导致测试失败,以及如何纠正它.