dart 没有与Mockito匹配的呼叫,尽管呼叫匹配,但仍进行验证

lmvvr0a8  于 2023-06-19  发布在  其他
关注(0)|答案(1)|浏览(97)

我有一个名为SqliteFavoriteProvider的提供程序,它有一个名为add的方法,如图所示:

@override
  Future<int> add({
    required int startStationId,
    required int endStationId,
    int? dayId,
  }) async {
    // `_database` here will be mocked
    return await _database.insert('favorites', {
      'startStationId': startStationId,
      'endStationId': endStationId,
      'dayId': dayId,
      'lastUpdate': clock.now().toIso8601String(),
    });
  }

我使用mockito模拟它,并测试它:

late FavoriteProvider provider;
    late Database mockDb;

    setupAll(() {
        mockDb = MockDatabase();
        // `mockDb` injected into SqliteFavoriteProvider
        provider = SqliteFavoriteProvider(database: mockDb);
    });

    // ...

    test('-- add (no day)', () {
      withClock(Clock.fixed(DateTime(2023)), () {
        provider.add(startStationId: 1, endStationId: 1);
      });
      verify(
        mockDb.insert(
          'favorites',
          {
            'startStationId': 1,
            'endStationId': 1,
            'dayId': null,
            'lastUpdate': '2023-01-01T00:00:00.0',
          },
          nullColumnHack: null, // doesn't work even if this is absent
          conflictAlgorithm: null, // doesn't work even if this absent
        ),
      );
    });

mockDbmockito生成的MockDatabase示例。
问题是,测试失败说明:

No matching calls. All calls: MockDatabase.insert('favorites', {startStationId: 1, endStationId: 1, dayId: null, lastUpdate: 2023-01-01T00:00:00.000}, {nullColumnHack: null, conflictAlgorithm: null})
  (If you called `verify(...).called(0);`, please instead use `verifyNever(...);`.)

但是,我用精确的参数调用它。我是不是漏掉了什么?

xsuvu9jc

xsuvu9jc1#

你可以像这样通过调用方法来检查它。

verify(
    mockDb.insert(
      'favorites',
      {
        'startStationId': 1,
        'endStationId': 1,
        'dayId': null,
        'lastUpdate': '2023-01-01T00:00:00.0',
      },
      nullColumnHack: null, // doesn't work even if this is absent
      conflictAlgorithm: null, // doesn't work even if this absent
    ),
  ).called(1);

相关问题