2014-03-06 113 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)); 
    }