跳到主內容

使用 Mockito 模擬依賴項

使用 Mockito 包來模擬服務行為以進行測試。

有時,單元測試可能依賴於從即時 Web 服務或資料庫獲取資料的類。 這存在一些不便之處

  • 呼叫即時服務或資料庫會降低測試執行速度。
  • 如果 Web 服務或資料庫返回意外結果,透過的測試可能會開始失敗。 這被稱為“不穩定的測試”。
  • 使用即時 Web 服務或資料庫很難測試所有可能的成功和失敗場景。

因此,與其依賴於即時 Web 服務或資料庫,您可以“模擬”這些依賴項。 模擬允許模擬即時 Web 服務或資料庫,並根據情況返回特定的結果。

一般來說,您可以透過建立類的替代實現來模擬依賴項。 手動編寫這些替代實現,或利用 Mockito 包 作為快捷方式。

本教程演示了使用以下步驟使用 Mockito 包進行模擬的基礎知識

  1. 新增包依賴項。
  2. 建立要測試的函式。
  3. 建立一個包含模擬 http.Client 的測試檔案。
  4. 為每種情況編寫一個測試。
  5. 執行測試。

有關更多資訊,請參閱 Mockito 包 文件。

1. 新增包依賴項

#

要使用 mockito 包,請將其新增到 pubspec.yaml 檔案中,以及 dev_dependencies 部分中的 flutter_test 依賴項。

此示例還使用 http 包,因此在 dependencies 部分中定義該依賴項。

mockito: 5.0.0 得益於程式碼生成,支援 Dart 的空安全。 要執行所需的程式碼生成,請在 dev_dependencies 部分中新增 build_runner 依賴項。

要新增依賴項,請執行 flutter pub add

flutter pub add http dev:mockito dev:build_runner

2. 建立要測試的函式

#

在此示例中,單元測試來自 從網際網路獲取資料 教程的 fetchAlbum 函式。 要測試此函式,請進行兩個更改

  1. 為函式提供一個 http.Client。 這允許根據情況提供正確的 http.Client。 對於 Flutter 和伺服器端專案,提供一個 http.IOClient。 對於瀏覽器應用,提供一個 http.BrowserClient。 對於測試,提供一個模擬 http.Client
  2. 使用提供的 client 從網際網路獲取資料,而不是難以模擬的靜態 http.get() 方法。

該函式現在應該如下所示

dart
Future<Album> fetchAlbum(http.Client client) async {
  final response = await client.get(
    Uri.parse('https://jsonplaceholder.typicode.com/albums/1'),
  );

  if (response.statusCode == 200) {
    // If the server did return a 200 OK response,
    // then parse the JSON.
    return Album.fromJson(jsonDecode(response.body) as Map<String, dynamic>);
  } else {
    // If the server did not return a 200 OK response,
    // then throw an exception.
    throw Exception('Failed to load album');
  }
}

在您的應用程式程式碼中,您可以直接使用 fetchAlbum(http.Client())http.Client 提供給 fetchAlbum 方法。 http.Client() 建立一個預設的 http.Client

3. 建立一個包含模擬 http.Client 的測試檔案

#

接下來,建立一個測試檔案。

遵循 單元測試簡介 教程中的建議,在根 test 資料夾中建立一個名為 fetch_album_test.dart 的檔案。

使用 @GenerateMocks([], customMocks: [MockSpec<http.Client>(as: #MockHttpClient)]) 註解來生成一個 MockHttpClient 類,並使用 mockito

生成的 MockHttpClient 類實現了 http.Client 類。 這允許您將 MockHttpClient 傳遞給 fetchAlbum 函式,並在每個測試中返回不同的 http 響應。

生成的模擬將位於 fetch_album_test.mocks.dart 中。 匯入此檔案以使用它們。

dart
import 'package:http/http.dart' as http;
import 'package:mocking/main.dart';
import 'package:mockito/annotations.dart';

// Generate a MockClient using the Mockito package.
// Create new instances of this class in each test.
// Note: Naming the generated mock `MockHttpClient` to avoid confusion with
// `MockClient` from `package:http/testing.dart`.
@GenerateMocks([], customMocks: [MockSpec<http.Client>(as: #MockHttpClient)])
void main() {
}

接下來,執行以下命令生成模擬

dart run build_runner build

4. 為每種情況編寫一個測試

#

fetchAlbum() 函式執行以下兩項操作之一

  1. 如果 http 呼叫成功,則返回一個 Album
  2. 如果 http 呼叫失敗,則丟擲一個 Exception

因此,您希望測試這兩種情況。 使用 MockHttpClient 類返回一個“Ok”響應以進行成功測試,並返回一個錯誤響應以進行不成功的測試。 使用 Mockito 提供的 when() 函式測試這些條件

dart
import 'package:flutter_test/flutter_test.dart';
import 'package:http/http.dart' as http;
import 'package:mocking/main.dart';
import 'package:mockito/annotations.dart';
import 'package:mockito/mockito.dart';

import 'fetch_album_test.mocks.dart';

// Generate a MockClient using the Mockito package.
// Create new instances of this class in each test.
// Note: Naming the generated mock `MockHttpClient` to avoid confusion with
// `MockClient` from `package:http/testing.dart`.
@GenerateMocks([], customMocks: [MockSpec<http.Client>(as: #MockHttpClient)])
void main() {
  group('fetchAlbum', () {
    test('returns an Album if the http call completes successfully', () async {
      final client = MockHttpClient();

      // Use Mockito to return a successful response when it calls the
      // provided http.Client.
      when(
        client.get(Uri.parse('https://jsonplaceholder.typicode.com/albums/1')),
      ).thenAnswer(
        (_) async =>
            http.Response('{"userId": 1, "id": 2, "title": "mock"}', 200),
      );

      expect(await fetchAlbum(client), isA<Album>());
    });

    test('throws an exception if the http call completes with an error', () {
      final client = MockHttpClient();

      // Use Mockito to return an unsuccessful response when it calls the
      // provided http.Client.
      when(
        client.get(Uri.parse('https://jsonplaceholder.typicode.com/albums/1')),
      ).thenAnswer((_) async => http.Response('Not Found', 404));

      expect(fetchAlbum(client), throwsException);
    });
  });
}

5. 執行測試

#

現在您已經有了帶有測試的 fetchAlbum() 函式,可以執行測試了。

flutter test test/fetch_album_test.dart

您還可以透過遵循 單元測試簡介 教程中的說明,在您最喜歡的編輯器中執行測試。

完整示例

#
lib/main.dart
#
dart
import 'dart:async';
import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;

Future<Album> fetchAlbum(http.Client client) async {
  final response = await client.get(
    Uri.parse('https://jsonplaceholder.typicode.com/albums/1'),
  );

  if (response.statusCode == 200) {
    // If the server did return a 200 OK response,
    // then parse the JSON.
    return Album.fromJson(jsonDecode(response.body) as Map<String, dynamic>);
  } else {
    // If the server did not return a 200 OK response,
    // then throw an exception.
    throw Exception('Failed to load album');
  }
}

class Album {
  final int userId;
  final int id;
  final String title;

  const Album({required this.userId, required this.id, required this.title});

  factory Album.fromJson(Map<String, dynamic> json) {
    return Album(
      userId: json['userId'] as int,
      id: json['id'] as int,
      title: json['title'] as String,
    );
  }
}

void main() => runApp(const MyApp());

class MyApp extends StatefulWidget {
  const MyApp({super.key});

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  late final Future<Album> futureAlbum;

  @override
  void initState() {
    super.initState();
    futureAlbum = fetchAlbum(http.Client());
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Fetch Data Example',
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
      ),
      home: Scaffold(
        appBar: AppBar(title: const Text('Fetch Data Example')),
        body: Center(
          child: FutureBuilder<Album>(
            future: futureAlbum,
            builder: (context, snapshot) {
              if (snapshot.hasData) {
                return Text(snapshot.data!.title);
              } else if (snapshot.hasError) {
                return Text('${snapshot.error}');
              }

              // By default, show a loading spinner.
              return const CircularProgressIndicator();
            },
          ),
        ),
      ),
    );
  }
}
test/fetch_album_test.dart
#
dart
import 'package:flutter_test/flutter_test.dart';
import 'package:http/http.dart' as http;
import 'package:mocking/main.dart';
import 'package:mockito/annotations.dart';
import 'package:mockito/mockito.dart';

import 'fetch_album_test.mocks.dart';

// Generate a MockClient using the Mockito package.
// Create new instances of this class in each test.
// Note: Naming the generated mock `MockHttpClient` to avoid confusion with
// `MockClient` from `package:http/testing.dart`.
@GenerateMocks([], customMocks: [MockSpec<http.Client>(as: #MockHttpClient)])
void main() {
  group('fetchAlbum', () {
    test('returns an Album if the http call completes successfully', () async {
      final client = MockHttpClient();

      // Use Mockito to return a successful response when it calls the
      // provided http.Client.
      when(
        client.get(Uri.parse('https://jsonplaceholder.typicode.com/albums/1')),
      ).thenAnswer(
        (_) async =>
            http.Response('{"userId": 1, "id": 2, "title": "mock"}', 200),
      );

      expect(await fetchAlbum(client), isA<Album>());
    });

    test('throws an exception if the http call completes with an error', () {
      final client = MockHttpClient();

      // Use Mockito to return an unsuccessful response when it calls the
      // provided http.Client.
      when(
        client.get(Uri.parse('https://jsonplaceholder.typicode.com/albums/1')),
      ).thenAnswer((_) async => http.Response('Not Found', 404));

      expect(fetchAlbum(client), throwsException);
    });
  });
}

概述

#

在此示例中,您已學習如何使用 Mockito 測試依賴於 Web 服務或資料庫的函式或類。 這只是 Mockito 庫和模擬概念的簡短介紹。 有關更多資訊,請參閱 Mockito 包 提供的文件。