lundi 18 mai 2020

Testing flutter app, cant wrap my head around this

Hey I m trying to get into widget testing, I wrote an App with dart and because i didnt have any clue how testing worked therefore i wasnt trying to build up my app for convenient testing while creating it.

I m trying to find textforms but cant find them anyhow.

 Widget build(BuildContext context) {
    categories = getCategoriesDropdownItems();
    return Scaffold(
      appBar: AppBar(
          title: Text(LanguageLocalizations.of(context).serviceEditPage),
          leading: IconButton(
              icon: Icon(Icons.arrow_back),
              onPressed: () {
                if(serviceManager.currentService != null )
                  serviceManager.focus(serviceManager.currentService);
                Navigator.pop(context);
              }),
          ),
      body: Container(
        padding: EdgeInsets.symmetric(horizontal: 20.0),
          child: SingleChildScrollView(
            child: Builder(
              builder: (originContext){
              return Column(
                children: <Widget>[
                  Container(child:
                    _titleTextFormField(),
                    key: Key('title'),),
                  _descriptionTextFormField(),
                  _categoryDropdownButton(),
                  _skillsTextFormField(),
                  Padding(padding: EdgeInsets.only(top: 20, bottom: 20),
                  child: Row(children: <Widget>[
                      _locationButtonsColumn(),
                      _locationsList()
                    ],)
                  ,),
                  _nextButton(originContext),
                ],
              );
              },
            ),
          ),
      ),
    );
  }
 Widget _titleTextFormField() {
    TextEditingController controller;
    if (serviceManager.justOpenedEditGeneralPage)
      controller = new TextEditingController(
          text: serviceManager.currentService != null
              ? serviceManager.currentService.title
              : '');
    return Padding(
        padding: EdgeInsets.only(top: 35.0),
        child: StreamBuilder(
          stream: serviceManager.title$,
          builder: (context, snapshot) {
            return TextFormField(
//              key: Key('title'),
                onChanged: serviceManager.inTitle.add,
                controller: controller,
                decoration: InputDecoration(
                    border: OutlineInputBorder(),
                    labelText: LanguageLocalizations.of(context).title,
                    hintText: LanguageLocalizations.of(context).titleHintText,
                    errorText: snapshot.error,
                    icon: Icon(Icons.title, color: Colors.grey)));
          },
        ));
  }

This is my page, I m trying to find TextFormFields after pumping up the page, either by key or my type neither seem to work

Widget makeTestableWidget({Widget child}) {
    LocalizationDelegate _localeOverrideDelegate = LocalizationDelegate(new Locale(ServiceControl.defaultLanguage, ''));
    Intl.defaultLocale = ServiceControl.defaultLanguage;
    return MaterialApp(
        localizationsDelegates: [
          _localeOverrideDelegate,
          GlobalMaterialLocalizations.delegate,
          GlobalWidgetsLocalizations.delegate,
        ],
        home: child,
      );
  }

testWidgets('Create Service', (WidgetTester tester) async {
    ServiceManager serviceManager = ServiceManager();
    serviceManager.reset();
    await tester.pumpWidget(makeTestableWidget(child: ServiceDetailEditingGeneralPage()));
    debugDumpApp();
//cant find anything tried finding the container in build, or the key in the textformfield method
    expect(find.byType(TextFormField), findsOneWidget);
    expect(find.byKey(Key('title')), findsOneWidget);
  });

This is the debugDumpApp() result

[root](renderObject: RenderView#f8484)
└MaterialApp(state: _MaterialAppState#441c7)
 └ScrollConfiguration(behavior: _MaterialScrollBehavior)
  └WidgetsApp-[GlobalObjectKey _MaterialAppState#441c7](state: _WidgetsAppState#baf25)
   └Shortcuts(manager: null, shortcuts: {LogicalKeySet#2c1db(keys: {LogicalKeyboardKey#7002b(keyId: "0x10007002b", keyLabel: null, debugName: "Tab")}): Intent#664ae(key: [<NextFocusAction>]), LogicalKeySet#1212e(keys: {LogicalKeyboardKey#700e1(keyId: "0x201000700e1", keyLabel: null, debugName: "Shift"), LogicalKeyboardKey#7002b(keyId: "0x10007002b", keyLabel: null, debugName: "Tab")}): Intent#9750b(key: [<PreviousFocusAction>]), LogicalKeySet#56cb4(keys: {LogicalKeyboardKey#70050(keyId: "0x100070050", keyLabel: null, debugName: "Arrow Left")}): DirectionalFocusIntent#4e0bd(key: [<DirectionalFocusAction>]), LogicalKeySet#0058e(keys: {LogicalKeyboardKey#7004f(keyId: "0x10007004f", keyLabel: null, debugName: "Arrow Right")}): DirectionalFocusIntent#1ed84(key: [<DirectionalFocusAction>]), LogicalKeySet#40831(keys: {LogicalKeyboardKey#70051(keyId: "0x100070051", keyLabel: null, debugName: "Arrow Down")}): DirectionalFocusIntent#d0b6b(key: [<DirectionalFocusAction>]), LogicalKeySet#a327d(keys: {LogicalKeyboardKey#70052(keyId: "0x100070052", keyLabel: null, debugName: "Arrow Up")}): DirectionalFocusIntent#eda6a(key: [<DirectionalFocusAction>]), LogicalKeySet#4b55e(keys: {LogicalKeyboardKey#70028(keyId: "0x100070028", keyLabel: null, debugName: "Enter")}): Intent#b0a3e(key: [<ActivateAction>]), LogicalKeySet#d93b2(keys: {LogicalKeyboardKey#00020(keyId: "0x00000020", keyLabel: " ", debugName: "Space")}): Intent#9d551(key: [<SelectAction>])}, state: _ShortcutsState#2c49e)
    └Focus(debugLabel: "Shortcuts", state: _FocusState#7dfe9)
     └_FocusMarker
      └Semantics(container: false, properties: SemanticsProperties, label: null, value: null, hint: null, hintOverrides: null, renderObject: RenderSemanticsAnnotations#7d611)
       └_ShortcutsMarker
        └Actions(dispatcher: null, actions: {[<DoNothingAction>]: Closure: () => DoNothingAction, [<RequestFocusAction>]: Closure: () => RequestFocusAction, [<NextFocusAction>]: Closure: () => NextFocusAction, [<PreviousFocusAction>]: Closure: () => PreviousFocusAction, [<DirectionalFocusAction>]: Closure: () => DirectionalFocusAction})
         └DefaultFocusTraversal
          └_MediaQueryFromWindow(state: _MediaQueryFromWindowsState#5bc7f)
           └MediaQuery(MediaQueryData(size: Size(800.0, 600.0), devicePixelRatio: 3.0, textScaleFactor: 1.0, platformBrightness: Brightness.light, padding: EdgeInsets.zero, viewPadding: EdgeInsets.zero, viewInsets: EdgeInsets.zero, physicalDepth: 1.7976931348623157e+308, alwaysUse24HourFormat: false, accessibleNavigation: false, highContrast: false,disableAnimations: false, invertColors: false, boldText: false))
            └Localizations(locale: en_US, delegates: [LocalizationDelegate[LanguageLocalizations], GlobalMaterialLocalizations.delegate(77 locales), GlobalWidgetsLocalizations.delegate(all locales), DefaultMaterialLocalizations.delegate(en_US), DefaultCupertinoLocalizations.delegate(en_US), DefaultWidgetsLocalizations.delegate(en_US)], dirty, state: _LocalizationsState#36639)
             └Container
              └LimitedBox(maxWidth: 0.0, maxHeight: 0.0, renderObject: RenderLimitedBox#3fd67)
               └ConstrainedBox(BoxConstraints(biggest), renderObject: RenderConstrainedBox#cd726)

no textformfields not my container with the key is inside

Aucun commentaire:

Enregistrer un commentaire