Dart unittest应该在例外情况下失败

时间:2014-03-06 13:57:23

标签: unit-testing asynchronous dart

我有这个测试:

     Future f = neo4d.nodes.delete(1);
      f.then(((_) {  
      })).catchError((e){
        expect(e.statusCode, equals(409));
      });
      return f;
    });

目前正在爆炸,因为e.statusCode是404而不是409.我希望测试失败但是由于未捕获的异常而导致整个测试套件停止。 我如何捕获异常(并且未通过测试)并阻止其爆炸所有其他测试?

这是我运行上面代码时得到的输出:

[2014-03-06 14:44:32.020] DEBUG http: R10: Received data in 9ms with status 404: [{
  "message" : "Cannot find node with id [1] in database.",
  "exception" : "NodeNotFoundException",
  "fullname" : "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)" ]
}]
Uncaught Error: Expected: 409
  Actual: 404

Stack Trace: 
#0      SimpleConfiguration.onExpectFailure (package:unittest/src/simple_configuration.dart:141:7)
#1      _ExpectFailureHandler.fail (package:unittest/src/simple_configuration.dart:15:28)
#2      DefaultFailureHandler.failMatch (package:unittest/src/expect.dart:117:9)
#3      expect (package:unittest/src/expect.dart:75:29)
#4      nodes... (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. (dart:async/future_impl.dart:367)
#14     _asyncRunCallback (dart:async/schedule_microtask.dart:18)
#15     _createTimer. (dart:async-patch/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)


Unhandled exception:
Expected: 409
  Actual: 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:async-patch/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)

关于奥斯卡

1 个答案:

答案 0 :(得分:3)

异步函数,即返回Future的函数,可以“抛出”两种不同的方式:

它可以同步抛出,甚至不会首先返回Future:

Future<int> doSomethingAsync() {
  throw new Exception("No! I don't want to even get started!");
}

或者,它可以异步抛出 - 即返回一个Future,然后异步抛出错误而不是完成:

Future<int> doSomethingAsync() {
  return new Future.error(new Exception("Here's your future, but it'll fail."));
}

您的异步电话

neo4d.nodes.delete(1)

必须是前一种类型:它甚至在没有返回Future的情况下立即抛出。这就是为什么异常不会被.catchError捕获,而是炸毁整个测试套件。

您想要更新neo4d.nodes.delete并使其异步抛出。或者,如果无法做到这一点,请将测试包装在一个良好的旧同步try-catch中:

  try {
    neo4d.nodes.delete(1).then(((_) { expect(0, 1);  }));
  }
  catch (e) {
    expect(e.statusCode, equals(409));
  }
相关问题