osk*_*bor 6 unit-testing asynchronous dart
我有这个测试:
未来 f = neo4d.nodes.delete(1); f.then(((_) { })).catchError((e){ 期望(e.statusCode,等于(409)); }); 返回 f; });
目前已经爆炸,因为是e.statusCode
404 而不是 409。我希望测试失败,但整个测试套件由于未捕获的异常而停止。如何捕获异常(并使测试失败)并阻止它破坏所有其他测试?
这是运行上面的代码时得到的输出:
[2014-03-06 14:44:32.020] 调试 http: R10: 在 9 毫秒内收到数据,状态为 404: [{ "message" : "在数据库中找不到 id [1] 的节点。", “异常”:“NodeNotFoundException”, “全名”:“org.neo4j.server.rest.web.NodeNotFoundException”, “stacktrace”:[“org.neo4j.server.rest.web.DatabaseActions.node(DatabaseActions.java:183)”,“org.neo4j.server.rest.web.DatabaseActions.deleteNode(DatabaseActions.java:233)” 、“org.neo4j.server.rest.web.RestfulGraphDatabase.deleteNode(RestfulGraphDatabase.java:279)”、“java.lang.reflect.Method.invoke(Method.java:601)”、“org.neo4j.server. rest.transactional.TransactionalRequestDispatcher.dispatch(TransactionalRequestDispatcher.java:139)", "org.neo4j.server.rest.security.SecurityFilter.doFilter(SecurityFilter.java:112)", "java.lang.Thread.run(Thread. java:722)"] }] 未捕获的错误:预期:409 实际:404 堆栈跟踪: #0 SimpleConfiguration.onExpectFailure (包:unittest/src/simple_configuration.dart:141:7) #1 _ExpectFailureHandler.fail (包:unittest/src/simple_configuration.dart:15:28) #2 DefaultFailureHandler.failMatch (包:unittest/src/expect.dart:117:9) #3 期望(包:unittest/src/expect.dart:75:29) #4 个节点... (file:///Users/oskbor/Projects/neo4dart/test/alltests.dart:202:15) #5 _invokeErrorHandler (dart:async/async_error.dart:12) #6 _Future._propagateToListeners。(dart:async/future_impl.dart:469) #7 _rootRun (dart:async/zone.dart:683) #8 _RootZone.run (dart:async/zone.dart:823) #9 _Future._propagateToListeners (dart:async/future_impl.dart:445) #10 _Future._propagateMultipleListeners (dart:async/future_impl.dart:384) #11 _Future._propagateToListeners (dart:async/future_impl.dart:411) #12 _Future._completeError (dart:async/future_impl.dart:315) #13 _Future._asyncCompleteError。(飞镖:异步/future_impl.dart:367) #14 _asyncRunCallback (dart:async/schedule_microtask.dart:18) #15 _createTimer。(dart:异步补丁/timer_patch.dart:11) #16 _Timer._createTimerHandler._handleTimeout (timer_impl.dart:151) #17 _Timer._createTimerHandler。(timer_impl.dart:166) #18 _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:93) 未处理的异常: 预计:409 实际:404 #0 _rootHandleUncaughtError.. (dart:async/zone.dart:677) #1 _asyncRunCallback (dart:async/schedule_microtask.dart:18) #2 _asyncRunCallback (dart:async/schedule_microtask.dart:21) #3 _createTimer。(dart:异步补丁/timer_patch.dart:11) #4 _Timer._createTimerHandler._handleTimeout (timer_impl.dart:151) #5 _Timer._createTimerHandler._handleTimeout (timer_impl.dart:159) #6 _Timer._createTimerHandler._handleTimeout (timer_impl.dart:159) #7 _Timer._createTimerHandler。(timer_impl.dart:166) #8 _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:93)
问候奥斯卡
异步函数(即返回 Future 的函数)可以通过两种不同的方式“抛出”:
它可以同步抛出,甚至不首先返回 Future:
Future<int> doSomethingAsync() {
throw new Exception("No! I don't want to even get started!");
}
Run Code Online (Sandbox Code Playgroud)
或者,它可以异步抛出- 即返回一个 Future,然后异步抛出错误而不是完成:
Future<int> doSomethingAsync() {
return new Future.error(new Exception("Here's your future, but it'll fail."));
}
Run Code Online (Sandbox Code Playgroud)
您的异步调用
neo4d.nodes.delete(1)
Run Code Online (Sandbox Code Playgroud)
必须是前一种类型:它立即抛出,甚至不返回 Future。这就是为什么异常不会被捕获.catchError
并炸毁整个测试套件。
您想要更新neo4d.nodes.delete
并使其异步抛出。或者,如果无法做到这一点,请将测试包装在一个好的旧同步 try-catch 中:
try {
neo4d.nodes.delete(1).then(((_) { expect(0, 1); }));
}
catch (e) {
expect(e.statusCode, equals(409));
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
779 次 |
最近记录: |