Unit testing 用throwsA进行Dart单元测试
使用Unit testing 用throwsA进行Dart单元测试,unit-testing,flutter,dart,exception,Unit Testing,Flutter,Dart,Exception,使用expect(method,throwsA) 因此,我有一个未来的函数,它正在执行一些任务,并在SocketEXception弹出时捕获它,然后抛出一个带有更好消息的异常 下面是一个示例代码,您可以使用它在测试时触发我的问题: Future testFunction()异步{ 试一试{ 抛出SocketException(“错误消息”); }关于SocketException捕获(41;{ 抛出异常(“我的自定义消息”); } } 测试('测试函数',()异步{ 期待( testFuncti
expect(method,throwsA)
因此,我有一个未来的函数,它正在执行一些任务,并在SocketEXception弹出时捕获它,然后抛出一个带有更好消息的异常
下面是一个示例代码,您可以使用它在测试时触发我的问题:
Future testFunction()异步{
试一试{
抛出SocketException(“错误消息”);
}关于SocketException捕获(41;{
抛出异常(“我的自定义消息”);
}
}
测试('测试函数',()异步{
期待(
testFunction(),
throwsA(例外(
“我的自定义消息”);
});
这是测试的结果:
Expected: throws _Exception:<Exception: My custom message>
Actual: <Instance of 'Future<void>'>
Which: threw _Exception:<Exception: My custom message>
应为:引发\u异常:
实际:
其中:引发了\u异常:
我不知道为什么测试没有按预期的那样运行,而实际的测试却抛出了完全相同的错误,我可能做错了什么,因为我是一个初学者,如果有人能帮助我理解为什么它不工作,那就太酷了
谢谢
import 'package:test/test.dart';
import 'dart:io';
void main() {
Future<bool> testFunction() async {
try {
throw SocketException('bad error message');
} on SocketException catch (_) {
throw Exception('My custom message');
}
}
test('test function', () async {
expect(
() async => await testFunction(),
throwsA(
(e) => e is Exception,
),
);
});
}
Connecting to VM Service at ws://127.0.0.1:43155/59iZNP08VJI=/ws
I/flutter ( 5523): 00:00 +0: test function
I/flutter ( 5523): 00:00 +1: All tests passed!