vendredi 8 février 2019

Flutter Widget Test - Zero Widget With Text Found

I have looked at YouTube, GitHub, official documentations, and Stackoverflow but still cannot figure out how to fix the following error for the below widget test. I just want to test the text inside the widget but cannot get it to work.

login.dart

class LoginPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      home: new LoginScreen(),
    );
  }
}

class LoginScreen extends StatefulWidget {
  @override
  LoginScreenState createState() => LoginScreenState();
}

class LoginScreenState extends State<LoginScreen> {

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      body: new Stack(
        children: <Widget>[
          new Center(
            child: new ListView(
              shrinkWrap: true,
              children: <Widget>[
                _email(),
                _passAWord(),
                new Text(
                  "Or",
                ),
              ],
            ),
          ),
        ],
      ),
    );
  }
}

widget_test.dart

testWidgets('Loginn test', (WidgetTester tester) async {
  await tester.pumpWidget(new LoginScreen());
  expect(find.text('Or'), findsOneWidget);
}); 

flutter test test/widget_test.dart error

The following assertion was thrown building LoginScreen(state: 
LoginScreenState#044d4):
MediaQuery.of() called with a context that does not contain a 
MediaQuery.
No MediaQuery ancestor could be found starting from the context that 
was passed to MediaQuery.of().
This can happen because you do not have a WidgetsApp or MaterialApp 
widget (those widgets introduce
a MediaQuery), or it can happen if the context you use comes from a 
widget above those widgets.
The context used was:
  Scaffold(dirty, state: ScaffoldState#8740c(lifecycle state: 
  initialized, tickers: tracking 1
  ticker))

The following TestFailure object was thrown running a test:
  Expected: exactly one matching node in the widget tree
  Actual: ?:<zero widgets with text "or" (ignoring offstage widgets)>
  Which: means none were found but one was expected

With the mediaQuery error I tried the following but it doesn't change anything.

 class LoginPage extends StatelessWidget {
 @override
 //  Widget build(BuildContext context) {
 //    return new MaterialApp(
 //      home: new LoginScreen(),
 //    );
 //  }
 Widget build(BuildContext context) {
  return new MediaQuery(
    data: new MediaQueryData(),
    child: new MaterialApp(
      home: new LoginScreen(),
    )
 );
}
}

Why is it complaining about mediaQuery in Scaffold() when there is MaterialApp in LoginPage()?

Aucun commentaire:

Enregistrer un commentaire