簡體   English   中英

我如何對多個期望進行單元測試

[英]how do I unit test with multiple expect

當有3個期望時,我在下面編寫的單元測試不起作用,我該如何重寫下面的單元測試:

test('400 bad response', () async {
  when(
    () => sampleClient.getTransaction(
        '1'),
  ).thenThrow(DioError(
      response: Response(
          statusCode: 400,
          data: badRepsonseJson,
          requestOptions: RequestOptions(path: '')),
      requestOptions: RequestOptions(path: '')));
  final call = sampleService.getTransactionByHash(
      '1');

  expect(() => call, throwsA(TypeMatcher<TatumException>())); // Expect 1
  try {
    await sampleService.getTransactionByHash(
        '1');
  } on TatumException catch (e) {
    expect(e.errorCode, badResponse.statusCode); // Expect 2
    expect(e.message, badResponse.message); // Expect 3
  }
});
final call = sampleService.getTransactionByHash( '1'); expect(() => call, throwsA(TypeMatcher<TatumException>())); // Expect 1

我不認為expect會成功。 調用() => call只是返回call變量已經計算的值,不會失敗。

如果您希望sampleService.getTransactionByHash('1'); 拋出一個異常,那么你需要在你的回調中調用它來expect

expect(
  () => sampleService.getTransactionByHash('1'),
  throwsA(TypeMatcher<TatumException>()),
);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM