gpt4 book ai didi

unit-testing - 如何使用 Mockito 在 Flutter 中模拟静态方法?

转载 作者:行者123 更新时间:2023-12-03 15:47:28 24 4
gpt4 key购买 nike

我有一个文件一个函数 fetchPosts()它负责从服务器获取新帖子并将它们存储在本地 sqlite 数据库中。

正如 sqflite 上的推荐 doc ,我将单个引用存储到我的数据库。

这是我的database.dart的内容文件:

import 'dart:async';
import 'package:path/path.dart';
import 'package:sqflite/sqflite.dart';

class DBProvider {
DBProvider._();
static final DBProvider db = DBProvider._();

static Database _database;

static Future<Database> get database async {
if (_database != null) return _database;
// if _database is null, we instantiate it
_database = await _initDB();
return _database;
}

static Future<Database> _initDB() async {
final dbPath = await getDatabasesPath();
String path = join(dbPath, 'demo.db');

return await openDatabase(path, version: 1, onCreate: _onCreate);
}

static Future<String> insert(String table, Map<String, dynamic> values) async { /* insert the record*/ }

// Other functions like update, delete etc.
}


然后我在我的 fetchPosts.dart 中使用它文件

import 'dart:convert';
import 'package:http/http.dart' as http;
import '../services/database.dart';

const url = 'https://myapp.herokuapp.com';

Future<void> fetchPosts() {
final client = http.Client();
return fetchPostsUsingClient(client);
}

Future<void> fetchPostsUsingClient(http.Client client) async {
final res = await client.get(url);
final posts await Post.fromJson(json.decode(response.body));

for (var i = 0; i < posts.length; i++) {
await DBProvider.insert('posts', posts[i]);
}
}

在我的测试中,我如何才能 verify那个 DBProvider.insert()已被调用?
fetchPosts_test.dart
import 'package:test/test.dart';
import 'package:http/http.dart' as http;
import 'package:mockito/mockito.dart';
import 'package:../services/fetchPosts.dart';

// Create a MockClient using the Mock class provided by the Mockito package.
// Create new instances of this class in each test.
class MockClient extends Mock implements http.Client {}

void main() {
group('fetchPosts', () {
test('update local db', () async {
final client = MockClient();

// Use Mockito to return a successful response when it calls the provided http.Client.
when(client.get()).thenAnswer((_) async => http.Response('{"title": "Test"}', 200));

await fetchPostsWithClient(client);

verify(/* DBProvider.insert has been called ?*/);
});
});
}

最佳答案

最终,我不得不重写我的 database.dart使其可测试/可模拟。
这是新文件:

import 'dart:async';
import 'package:path/path.dart';
import 'package:sqflite/sqflite.dart';

class DBProvider {
static final DBProvider _singleton = DBProvider._internal();

factory DBProvider() {
return _singleton;
}

DBProvider._internal();

static Database _db;

static Future<Database> _getDatabase() async {
if (_db != null) return _db;
// if _database is null, we instantiate it
_db = await _initDB();
return _db;
}

static Future<Database> _initDB() async {
final dbPath = await getDatabasesPath();
String path = join(dbPath, 'demo.db');

return openDatabase(path, version: 1, onCreate: _onCreate);
}

Future<String> insert(String table, Map<String, dynamic> values) async {
final db = await _getDatabase();
return db.insert(table, values);
}

// ...
}

现在我可以使用与 http.Client 相同的技巧。
谢谢@RémiRousselet

关于unit-testing - 如何使用 Mockito 在 Flutter 中模拟静态方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58238435/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com