Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to include a Provider(s) within widget testing?

I have a Widget that relies on a provider. I want to run widget tests on this widget. I assume because widget testing is directly at that node in the tree, it is reasonable to get errors such as:

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

(note: the name of my Provider is . May main includes:

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

Is there a syntax/way to include the Provider when running a widget test on a widget lower down the widget tree?

like image 992
user1255603 Avatar asked Aug 27 '19 12:08

user1255603


1 Answers

You could try something like this:

    await tester.pumpWidget(MultiProvider(
         providers: [
        Provider<AuthBase>(
          builder: (context) => Auth(),
        ),
        Provider<Appointments>(
          builder: (context) => Appointments(),
         )
       ],
      child: Builder(
        builder: (_) => YourWidgeToTest(),
      ),
    ),);
like image 118
diegoveloper Avatar answered Oct 13 '22 13:10

diegoveloper