如何在小部件测试中获取变量?

时间:2019-09-14 09:17:07

标签: unit-testing flutter dart widget

我正在将名为givenProfilePictureURL的变量传递到AppDrawer中。然后将此变量设置为NetworkImage。我知道测试中的NetworkImage必须被模拟,但是我只想验证该变量,因为它带有一些逻辑。

这是我目前正在进行的测试

  testWidgets('With a givenProfilePictureURL and a givenFirstName', (WidgetTester tester) async {
    provideMockedNetworkImages(() async {
      String givenProfilePictureURL = 'https://GarbageUrl.com/image.png';
      String givenFirstName = 'Bob';

      final GlobalKey<ScaffoldState> scaffoldKey = GlobalKey<ScaffoldState>();

      final Widget materialApp = MaterialApp(
          home: Scaffold(
            key: scaffoldKey,
            drawer: AppDrawer(givenProfilePictureURL, givenFirstName),
          ));

      await tester.pumpWidget(materialApp);

      scaffoldKey.currentState.openDrawer();
      await tester.pumpAndSettle();

      final appDrawerFinder = find.byKey(Key('drawerDrawer'));
      final firstNameFinder = find.text('Bob');
      final raisedButtonFinder = find.text('Log in');
      //How can I verify that givenProfilePictureURL is in my widget?

      expect(appDrawerFinder, findsOneWidget);
      expect(firstNameFinder, findsOneWidget);
      expect(raisedButtonFinder, findsNothing);
    });
  });

这是AppDrawer小部件的顶部

 Widget build(BuildContext context) {
    String profilePicture = Validator.isNotNullEmptyFalseOrZero(givenProfilePictureURL)
        ? givenProfilePictureURL
        : "http://randomurl.com/asdsa.png";

    String firstName = Validator.isNotNullEmptyFalseOrZero(givenFirstName) ? givenFirstName : "Guest";

    return Drawer(
      key: Key('drawerDrawer'),
      child: Column(
        children: <Widget>[
          Expanded(
            child: ListView(
              children: <Widget>[
                Column(
                  children: <Widget>[
                    Container(
                        margin: EdgeInsets.symmetric(horizontal: 102.0),
                        height: 100.0,
                        decoration: new BoxDecoration(
                            border: new Border.all(
                              color: ThemeSettings.AvatarBackgroundColor,
                              width: 1.0,
                            ),
                            shape: BoxShape.circle,
                            image: DecorationImage(fit: BoxFit.cover, image: NetworkImage(profilePicture)))),

我知道有诸如find.descent之类的东西,但是如何在测试中获取NetworkImage中的profilePicture的值?

谢谢!

0 个答案:

没有答案