Dart 颤振:测试是否引发特定异常

Dart 颤振:测试是否引发特定异常,dart,flutter,flutter-test,Dart,Flutter,Flutter Test,简而言之,throwsA(anywhere)对我来说在dart中进行单元测试是不够的。如何测试特定的错误消息或类型 下面是我想要捕捉的错误: class MyCustErr implements Exception { String term; String errMsg() => 'You have already added a container with the id $term. Duplicates are not allowed'; MyCustErr(

简而言之,
throwsA(anywhere)
对我来说在dart中进行单元测试是不够的。如何测试特定的错误消息或类型

下面是我想要捕捉的错误:

class MyCustErr implements Exception {
  String term;

  String errMsg() => 'You have already added a container with the id 
  $term. Duplicates are not allowed';

  MyCustErr({this.term});
}
以下是通过的当前断言,但希望检查上面的错误类型:

expect(()=>operations.lookupOrderDetails(),throwsA(anything))

这就是我想做的:


expect(()=>operations.lookupOrderDetails(),throwsA(MyCustErr))

这应该满足您的要求:

expect(() => operations.lookupOrderDetails(), throwsA(const TypeMatcher<MyCustErr>()));

expect(() => operations.lookupOrderDetails(), isInstanceOf<MyCustErr>());
expect(()=>operations.lookupOrderDetails(),throwsA(consttypematcher());
expect(()=>operations.lookupOrderDetails(),isInstanceOf());

如果您只想检查是否存在异常,请检查以下内容:

如果有人想像我一样使用异步函数进行测试,您需要做的就是在expect中添加
async
关键字,记住
lookupOrderDetails
是一个异步函数:

expect(() **async** => **await** operations.lookupOrderDetails(), throwsA(const TypeMatcher<MyCustErr>()));

expect(() **async** => **await** operations.lookupOrderDetails(), isInstanceOf<MyCustErr>()));
expect(()**异步**=>**等待**操作。lookupOrderDetails(),throwsA(constTypeMatcher());
expect(()**异步**=>**等待**操作。lookupOrderDetails(),isInstanceOf());

它仍然使用冈特的答案,这是非常好的

首先导入正确的软件包“软件包:matcher/matcher.dart”

expect(() => yourOperation.yourMethod(),
      throwsA(const TypeMatcher<YourException>()));
expect(()=>yourOperation.yourMethod(),
throwsA(consttypematcher());
在Flatter 1.12.1中弃用了“TypeMatcher”之后,我发现这是可行的:

expect(() => operations.lookupOrderDetails(), throwsA(isInstanceOf<MyCustErr>()));
expect(()=>operations.lookupOrderDetails(),throwsA(isInstanceOf());

当前期望函数调用引发异常的正确方法是:

expect(operations.lookupOrderDetails, throwsA(isA<MyCustErr>()));`
expect(operations.lookupOrderDetails,throwsA(isA())`

从2021年4月起,这是正确的方法

正确的方法

import 'package:dcli/dcli.dart';
import 'package:test/test.dart';

 /// GOOD: works in all circumstances.
 expect(() => restoreFile(file), throwsA(isA<RestoreFileException>()));
import 'package:dcli/dcli.dart';
import 'package:test/test.dart';
 /// BAD: works but not in all circumstances
 expect(restoreFile(file), throwsA(isA<RestoreFileException>()));

还有一个
匹配器实例。这对我不起作用。但是,可能是由于我的设置:
预期:实际:动态>
删除闭包不起作用,也不适用于我,但只要我有其他选择。非常感谢。作为对未来人们的提醒,我无法找到工作的第一选择。我发现这是因为VS Code Intellisense让我在实际需要导入'package:matcher/matcher.dart'时导入了'package:flatter/{widgets material cupertino}.dart'。在flatter 1.12.1以后的版本中,TypeMatcher已被弃用。对我来说,这个匹配器是有效的:
throwsA(isInstanceOf())
你也可以在这篇文章中找到关于如何匹配特定消息的答案:不知道为什么,但这两个变体对我都不起作用,但是
expect(()async=>wait-resultFuture,throwsA(isInstanceOf())
did
isInstanceOf
也被弃用,取而代之的是
isA
。因此代码现在应该是
expect(()=>operations.lookupOrderDetails(),throwsA(isA())这当然有效,但没有逻辑上的理由让它通过lambdas!与JUnit之类的东西相比,这是直接的非直观语法。@箭头如果被调用的方法返回null,则必须使用()否则会出现编译错误:此表达式的类型为“void”,因此无法使用其值。。。我确实把棉绒调到了11,这可能就是为什么你看到了不同。
import 'package:dcli/dcli.dart';
import 'package:test/test.dart';

    expect(
        () => copy(from, to),
        throwsA(predicate((e) =>
            e is CopyException &&
            e.message == 'The from file ${truepath(from)} does not exists.')));