3

僅當存在某些未決請求時纔可以調用$httpbackend.flush();? 所以我永遠不會得到

Error: Unflushed requests: 1,2,3,...,n

或者

Error: No pending request to flush !

回答

2

按照documentation有,你可以使用一個$http.pendingRequests屬性。像這樣的東西會工作:

if($http.pendingRequests.length > 0) { 
    $httpBackend.flush(); 
} 

我不知道它是個非常不錯的主意,但應該這樣做。

+0

沒有如果的請求時它不工作 – Stevik

1

我認爲你應該組織你的測試不要在測試中使用任何「if」。 爲什麼? 爲了讓它簡單易懂,並且易於理解實際測試的內容,「if」提供了一種在失敗時通過測試的方法。

當沒有向API發出請求時,將單獨的測試函數寫入測試用例。

閱讀關於AAA(Arrange Act Assert)模式在測試它會幫助你。

+0

在某些情況下,決定是否是一個實現細節。測試不應該假設,實施過程中發生了什麼 – hansmaad

1

如果您不「期待」任何請求,則可以將呼叫置於http.flush(n)的try-catch塊中,以忽略該異常。

http.whenGet(/* .. */).respond(/*..*/); // maybe implementation needs some data 

service.doSomething(); 

try { http.flush(99); } // resolve all the possible requests my service might have to do 
catch(e) {} 

expect(service.isAwesome).toBe(true); 
相關問題