【问题标题】:How to include a Provider(s) within widget testing?如何在小部件测试中包含提供者?
【发布时间】:2019-12-31 15:33:43
【问题描述】:

我有一个依赖于提供程序的小部件。我想在这个小部件上运行小部件测试。我假设因为小部件测试直接在树中的那个节点上,所以得到以下错误是合理的:

  * Ensure the Provider<Appointments> is an ancestor to this InstallMonitorPage Widget

(注意:我的 Provider 的名字是 . 可能主要包括:

 return MultiProvider(
      providers: [
        Provider<AuthBase>(
          builder: (context) => Auth(),
        ),
        Provider<Appointments>(
          builder: (context) => Appointments(),
        )
      ],
      child: MaterialApp(
          title: title,
          theme: ThemeData(
            primarySwatch: Colors.blue,
          ),
          home: LaunchPage()),
    );

在小部件树下的小部件上运行小部件测试时,是否有包含提供程序的语法/方式?

【问题讨论】:

    标签: flutter flutter-test


    【解决方案1】:

    你可以试试这样的:

        await tester.pumpWidget(MultiProvider(
             providers: [
            Provider<AuthBase>(
              builder: (context) => Auth(),
            ),
            Provider<Appointments>(
              builder: (context) => Appointments(),
             )
           ],
          child: Builder(
            builder: (_) => YourWidgeToTest(),
          ),
        ),);
    

    【讨论】:

    • 谢谢。当我尝试这个时(builder: (_) =&gt; MaterialApp( home: InstallMonitorPage(), ), 我陷入了The following assertion was thrown during layout: A RenderFlex overflowed by 106 pixels on the bottom. The overflowing RenderFlex has an orientation of Axis.vertical. 的兔子洞,我尝试使用各种灵活和扩展的小部件进行包装,但一定是一无所知。有没有明显的方法来追踪这个挑战?
    • 好吧,您的布局似乎有问题,请记住,当您测试您的应用时,它会在不同屏幕尺寸的“模拟器”中运行
    猜你喜欢
    • 2020-08-09
    • 2021-03-02
    • 2021-08-28
    • 2020-09-27
    • 2023-04-01
    • 2016-05-04
    • 2021-11-13
    • 2021-12-08
    • 2020-10-16
    相关资源
    最近更新 更多