2014-03-06 38 views
3

我有这样的测试:飞镖单元测试应该失败的例外

 
    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) 

关于奥斯卡

回答

3

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

它可以扔同步,甚至没有在第一时间返回未来:

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

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

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 { 
    neo4d.nodes.delete(1).then(((_) { expect(0, 1); })); 
    } 
    catch (e) { 
    expect(e.statusCode, equals(409)); 
    }