Flutter测试无尾错误错误:键入' null'类型的子类型不是Future< object?>'

发布于 2025-02-03 03:48:23 字数 2466 浏览 3 评论 0原文

我想测试当按钮10次时是否按下路线。当测试的小部件调用modular.to.pushnamed()方法时,将以下错误丢弃了

The following _TypeError was thrown running a test:
type 'Null' is not a subtype of type 'Future<Object?>'

When the exception was thrown, this was the stack:
#0      ModularNavigateMock.pushNamed (package:flutter_modular/src/presenter/models/modular_navigator.dart:40:14)
#1      AgileSmellsState.goToResults (package:serious_agile_games/agile-smells/agile_smells.dart:37:16)
#2      AgileSmellsState.build.<anonymous closure> (package:serious_agile_games/agile-smells/agile_smells.dart:50:26)
#3      SwipingDeck.swipeRight (package:swiping_card_deck/swiping_card_deck.dart:119:38)
<asynchronous suspension>

呼叫,将异常抛出agileamells widget widget in

void goToResults() {
    Modular.to.pushNamed(AgileSmellsResults.route, arguments: validatedSmells);
  }

vardicatedsmells是一个不可nulable list.Empty(growable:true)

我的测试是

testWidgets('Test validate smell 10 times', (tester) async {
      await tester.pumpWidget(TestModularApp(widgetToTest: AgileSmells(firestore: firestore)));

      await tester.pumpAndSettle();

      final deck = find.byType(SwipingDeck<SmellCard>);
      final validateButton = find.byIcon(Icons.check);

      expect(deck, findsOneWidget);
      expect(validateButton, findsOneWidget);

      await tester.tap(validateButton);
      await tester.pumpAndSettle();
      verifyNever(
        () => TestModularNavigation.mockedNavigator.pushNamed(AgileSmellsResults.route, arguments: any, forRoot: false),
      );

      for (int i = 1; i < 10; i++) {
        await tester.tap(validateButton);
        await tester.pumpAndSettle();
      }
      // Verify causing failure in the test
      verify(
        () => TestModularNavigation.mockedNavigator.pushNamed(AgileSmellsResults.route, arguments: any, forRoot: false),
      ).called(1); 
    });

我模拟的ModularNavigate类,

class ModularNavigateMock extends Mock implements IModularNavigator {}

class TestModularNavigation {
  static final mockedNavigator = ModularNavigateMock();

  static void setUp() {
    Modular.navigatorDelegate = mockedNavigator;
    when(() => mockedNavigator.pushNamed(AgileSmellsResults.route, arguments: any)).thenAnswer((_) async => const AgileSmellsResults());
  }
}

谢谢您帮助我

I want to test if a route is pushed when a button is tapped 10 times. When the Modular.to.pushNamed() method is called by the tested widget, the following error was thrown

The following _TypeError was thrown running a test:
type 'Null' is not a subtype of type 'Future<Object?>'

When the exception was thrown, this was the stack:
#0      ModularNavigateMock.pushNamed (package:flutter_modular/src/presenter/models/modular_navigator.dart:40:14)
#1      AgileSmellsState.goToResults (package:serious_agile_games/agile-smells/agile_smells.dart:37:16)
#2      AgileSmellsState.build.<anonymous closure> (package:serious_agile_games/agile-smells/agile_smells.dart:50:26)
#3      SwipingDeck.swipeRight (package:swiping_card_deck/swiping_card_deck.dart:119:38)
<asynchronous suspension>

The call throwing the exception in AgileAmells widget is

void goToResults() {
    Modular.to.pushNamed(AgileSmellsResults.route, arguments: validatedSmells);
  }

Where validatedSmells is a not nullable List.empty(growable: true)

My test is

testWidgets('Test validate smell 10 times', (tester) async {
      await tester.pumpWidget(TestModularApp(widgetToTest: AgileSmells(firestore: firestore)));

      await tester.pumpAndSettle();

      final deck = find.byType(SwipingDeck<SmellCard>);
      final validateButton = find.byIcon(Icons.check);

      expect(deck, findsOneWidget);
      expect(validateButton, findsOneWidget);

      await tester.tap(validateButton);
      await tester.pumpAndSettle();
      verifyNever(
        () => TestModularNavigation.mockedNavigator.pushNamed(AgileSmellsResults.route, arguments: any, forRoot: false),
      );

      for (int i = 1; i < 10; i++) {
        await tester.tap(validateButton);
        await tester.pumpAndSettle();
      }
      // Verify causing failure in the test
      verify(
        () => TestModularNavigation.mockedNavigator.pushNamed(AgileSmellsResults.route, arguments: any, forRoot: false),
      ).called(1); 
    });

My mocked ModularNavigate class

class ModularNavigateMock extends Mock implements IModularNavigator {}

class TestModularNavigation {
  static final mockedNavigator = ModularNavigateMock();

  static void setUp() {
    Modular.navigatorDelegate = mockedNavigator;
    when(() => mockedNavigator.pushNamed(AgileSmellsResults.route, arguments: any)).thenAnswer((_) async => const AgileSmellsResults());
  }
}

Thank you for helping me

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

知你几分 2025-02-10 03:48:23

通过阅读“ noreferrer”>“ noreflow noreferrer”> oilbtail faq和pub.dev上的文档

这个问题是错误的问题。代码>推名方法。我更改了参数: by 参数:any(ney(naty:'grauments')在我的mockednavigatorverify> verify < /代码>步骤

我的修复MockedNavigator

class ModularNavigateMock extends Mock implements IModularNavigator {}

class TestModularNavigation {
  static final mockedNavigator = ModularNavigateMock();

  static void setUp() {
    Modular.navigatorDelegate = mockedNavigator;
    when(() => mockedNavigator.pushNamed(AgileSmellsResults.route, arguments: any(named: 'arguments'))).thenAnswer((_) async => const AgileSmellsResults(validatedSmellCards: []));
  }
}

Solved by reading the Mocktail FAQ and documentation on pub.dev

The problem was wrong stubbing of pushNamed method. I changed arguments: any by arguments: any(named: 'arguments') for correct stubbing in my mockedNavigator and in verify steps

My fixed mockedNavigator

class ModularNavigateMock extends Mock implements IModularNavigator {}

class TestModularNavigation {
  static final mockedNavigator = ModularNavigateMock();

  static void setUp() {
    Modular.navigatorDelegate = mockedNavigator;
    when(() => mockedNavigator.pushNamed(AgileSmellsResults.route, arguments: any(named: 'arguments'))).thenAnswer((_) async => const AgileSmellsResults(validatedSmellCards: []));
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文