0

在初始状态下,访问视图时,必须禁用按钮,在执行特定操作后,必须启用按钮。关于如何测试这种行为的任何想法?

4

1 回答 1

0

您可以使用tester.widget<ElevatedButton>(buttonFinder).enabled

这是我之前在同一场景中进行的测试的完整示例:

testWidgets('Button disabled when loading and enabled after result',
          (WidgetTester tester) async {
        LocationBlocMock locationBlocMock = LocationBlocMock();
    
        await tester.pumpWidget(
          MaterialApp(
            home: Builder(
              builder: (context) {
                return BlocProvider<LocationBloc>(
                  create: (BuildContext context) => locationBlocMock,
                  child: LocationAccess(),
                );
              },
            ),
          ),
        );
    
        locationBlocMock.add(GetCurrentLocation());
    
        await tester.pump(const Duration(seconds: 1));
    
        final buttonFinder = find.byKey(const Key("give_permission_key"));
    
        expect(buttonFinder, findsOneWidget);
        expect(tester.widget<ElevatedButton>(buttonFinder).enabled, isFalse);
    
        await tester.pump(const Duration(seconds: 1));
    
        expect(tester.widget<ElevatedButton>(buttonFinder).enabled, isTrue);
    
        locationBlocMock.close();
      });
于 2021-12-30T21:13:06.257 回答