我有一个名为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
),
);
});
mockDb
是mockito
生成的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(...);`.)
但是,我用精确的参数调用它。我是不是漏掉了什么?
1条答案
按热度按时间xsuvu9jc1#
你可以像这样通过调用方法来检查它。