1

例外をスローするコードに関するテストを作成する場合、Dart/Mockito (またはその他のもの) が実際の例外をスローしないようにするにはどうすればよいでしょうか? たとえば、これらのテストは両方とも合格し、スローされた例外を検出する必要がありますが、Dart は最初のテストで実際の例外をスローするため、「Todo を受け取る」だけが合格します。

void main() {
  test('It throws an exception', () async {
    final client = MockClient();
    when(client.get(Uri.parse('https://jsonplaceholder.typicode.com/todos/1'))).thenAnswer((_) async => http.Response('', 404));
    expect(await fetchTodo(client, 1), throwsException);
  });

  test('It receives a Todo', () async {
    final client = MockClient();
    final jsonString = '''
    {
      "id": 1,
      "userId": 1,
      "title": "test",
      "completed": false
    }
    ''';
    when(client.get(Uri.parse('https://jsonplaceholder.typicode.com/todos/2'))).thenAnswer((_) async => http.Response(jsonString, 200));
    expect(await fetchTodo(client, 2), isA<Todo>());
  });
}

そしてモックされたgetメソッド(mockitoの生成コードに基づく-@GenerateMocks([http.Client])テストファイルで使用すると同じ結果が得られます。

class MockClient extends Mock implements http.Client {
  Future<http.Response> get(Uri url, {Map<String, String>? headers}) {
    return super.noSuchMethod(Invocation.method(#get, [url], {#headers: headers}), returnValue: Future.value(http.Response('', 200))) as Future<http.Response>;
  }
}
class Todo {
  int id;
  int userId;
  String title;
  bool completed;

  Todo(this.id, this.userId, this.title, this.completed);
}

Future<Todo> fetchTodo(http.Client client, int id) async {
  final response = await client.get(Uri.parse('https://jsonplaceholder.typicode.com/todos/$id'));

  if(response.statusCode == 200) {
    return Todo(1, 1, 'Test', true);
  }else {
    throw Exception('Failed to fetch resource');
  }
}

テスト実行レポート:

00:00 +0: It throws an exception
00:00 +0 -1: It throws an exception [E]
  Exception: Failed to fetch resource
  test/test.dart 49:5  fetchTodo
  
00:00 +0 -1: It receives a Todo
00:00 +1 -1: Some tests failed.
4

1 に答える 1