如何在颤振中模拟数据库

我试图模拟数据库来测试我的本地 api,我在官方文档中搜索找到可以很好地与远程 api 一起使用的 mockito,但也不能与本地数据库开箱即用,有什么办法可以解决它吗?

回答

在这些情况下,您有两种选择(以及许多其他选择)。即使我的示例假设您正在进行 HTTP 调用,也没关系。无论我公开的具体用例是什么,您都可以使用这些策略!


第一个是使用“策略模式”为 API 创建接口,然后在测试 API 和生产 API 之间切换。这是一个简单的例子:

abstract class HttpRepository {
  const HttpRepository();

  Future<Something> sendRequest();
}

您现在可以创建 2 个具体的类:一个用于实际的 API 调用,另一个只是用于测试的模拟。

/// Use this in your tests
class MockHttpRepository extends HttpRepository {
  const MockHttpRepository();

  @override
  Future<Something> sendRequest() async {
    // Simulating the HTTP call
    await Future.delayed(const Duration(seconds: 2));
    return Something();
  }
}

/// Use this in your Flutter code to make the actual HTTP call or whatever else
class ApiHttpRepository extends HttpRepository {
  const ApiHttpRepository();

  @override
  Future<Something> sendRequest() async {
    // Doing a real HTTP call
    final response = await makeGetOrPost();
    return Something.withData(response);
  }
}

这样,您将ApiHttpRepository在 Flutter 应用程序和MockHttpRepository测试中使用。const尽可能使用构造函数。


另一种方法是使用模拟来模拟虚假的 HTTP 调用或其他任何东西。基本上,您使用的when是“捕获”方法调用并返回您可以控制的假响应。

// 1. "Enable" mocking on your type
class MockRepo extends Mock implements ApiHttpRepository {}

// 2. Mock methods
const ApiHttpRepository repo = MockRepo();
when(repo.sendRequest()).thenAnswer((_) async => Something());

在这种情况下,我们使用的thenAnswer是因为 的返回类型sendRequest()是 type Future<T>。在您的情况下,如果您从数据库读取数据,您只需要:

  1. 使用 extends Mock implements YourClass
  2. 使用when上mockable实例和控制输出

确保thenAnswer在方法返回 a 时使用Future<T>thenReturn在所有其他情况下使用。


以上是如何在颤振中模拟数据库的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>