0

我想知道是否有人可以帮助我,我在我的颤振项目上为我的主页编写测试时遇到了麻烦,因为它依赖于提供者。

home_page.dart

Future<NewsModel> newsData = Provider.of<Future<NewsModel>>(context);

return FutureProvider(
  create: (context) => newsData,
  child: (newsData != null)
      ? Scaffold(
          body: SmartRefresher(
            controller: _refreshController,
            onRefresh: _refreshNews,
            onLoading: _onLoading,
            child: Consumer<NewsModel>(
              builder: (_, news, __) {...}

主要.dart

return MultiProvider(
  providers: [
    FutureProvider(create: (context) => geolocatorService.getlocation()),
    ProxyProvider<Position, Future<NewsModel>>(
      update: (context, position, news) {
        return (position != null) ? newsService.getNews() : null;
      },
    ),

home_page_test.dart

testWidgets('Test the home screen', (WidgetTester tester) async {
HomePage homePage = new HomePage();

await tester.pumpWidget(
  makeWidgetTestable(
    child: homePage,
  ),
);

}); }

当我运行这个测试时,我收到以下错误,我真的很困惑,需要一些指导。我对 Flutter 相当有经验,但我是为需要外部依赖项的小部件编写小部件测试的新手。如果有人可以提供帮助,那就太好了。

错误:

The following ProviderNotFoundException was thrown building HomePage(dirty, dependencies [MediaQuery], state: _HomePageState#ce244): Error: Could not find the correct Provider<Future<NewsModel>> above this HomePage Widget
4

1 回答 1

2

如果您使用主文件中的提供程序包装以下代码:

await tester.pumpWidget(
  makeWidgetTestable(
    child: homePage,
  ),
);

并将其变成:

testWidgets('Test the home screen', (WidgetTester tester) async {
//No longer need the following line
//HomePage homePage = new HomePage();

await tester.pumpWidget(
MultiProvider(
  providers: [
    FutureProvider(create: (context) => geolocatorService.getlocation()),
    ProxyProvider<Position, Future<NewsModel>>(
      update: (context, position, news) {
        return (position != null) ? newsService.getNews() : null;
      },
    ),
    child: homePage,
  ),
);
于 2021-02-05T20:56:26.400 回答