2014-03-13 110 views
3

我最近開始從茉莉花1.3遷移到2.0,並遇到一些問題。從Jasmine 1.3遷移到2.0的問題done和SetTimeOut的問題

這是我的舊的測試怎麼看起來像:

it("should start heartbeat after successful login and stop heartbeat after logout", function() { 
    runs(function() { 
     auth.hbTimeout = 500; 
     var loggedIn = auth.login("USERWITHSESSION", "xyz", {}); 
     expect(loggedIn).toBe(true); 
     expect(auth.getAuthenticated()).toBe(true); 
     expect(auth.user).toBeDefined(); 
     expect(auth.user.Session).toEqual(74790750); 
     setTimeout(function() { 
      auth.stopHeartbeat(); 
      auth.user.Session = 74790760; 
     }, 2000); 
    }); 
    waitsFor(function() { 
     return auth.user.Session == 74790760; 
    }, "The session-id should have been changed", 2600); 
    runs(function() { 
     auth.heartbeat(); 
     expect(auth.getAuthenticated()).toBe(false); 
     expect(auth.user).not.toBeDefined(); 
     auth.login("USERWITHSESSION", "xyz", {}); 
     setTimeout(function() { 
      auth.user.Session = 74790750; 
     }, 500); 
    }); 
    waitsFor(function() { 
     return auth.user.Session == 74790750; 
    }, "The session-id should have been changed back", 1100); 
    runs(function() { 
     setTimeout(function() { 
      auth.logout(); 
     }, 2000); 
    }); 
    waitsFor(function() { 
     return auth.getAuthenticated() == false; 
    }); 
    expect(auth.user).not.toBeDefined(); 
}); 

我要複製的部分,直到第waitsFor()。對於第二次超時,我嘗試了setTimout()並將期望值移至afterEach。

據我瞭解,茉莉花應該等待兩秒鐘然後執行代碼,仍然期望總是錯誤的,測試失敗。

這是我做的:

describe("this is a async nested describe",function(){ 

    afterEach(function(done){ 
     expect(auth.user.Session).toBe(74790760); 
    }); 

    it("let's do this",function(){ 

     auth.hbTimeout = 500; 
     var loggedIn = auth.login("USERWITHSESSION", "xyz", {}); 
     expect(loggedIn).toBe(true); 
     expect(auth.getAuthenticated()).toBe(true); 
     expect(auth.user).toBeDefined(); 
     expect(auth.user.Session).toEqual(74790750); 


     setTimeout(function() { 
      auth.stopHeartbeat(); 
      auth.user.Session = 74790760; 
      done(); 
     },2000); 
    }); 

}); 

我相信我在做什麼愚蠢錯誤。有人可以給我一個提示嗎?哦,不管我做什麼,即使我將超時設置爲一分鐘,測試仍然在相同的時間內達到預期。

提前致謝!

回答

3

您沒有將done函數傳遞到您的let's do this規範中。 Jasmine 2.0根據規範函數的length屬性將規範運行爲同步或異步,因此無參數函數將始終運行同步。

以下代碼來自Jasmine's GitHub(/src/core/QueueRunner.js)。

for(iterativeIndex = recursiveIndex; iterativeIndex < length; iterativeIndex++) { 
    var fn = fns[iterativeIndex]; 
    if (fn.length > 0) { 
    return attemptAsync(fn); 
    } else { 
    attemptSync(fn); 
    } 
} 

另外,不要忘了也呼籲done()在afterEach功能,像這樣:

afterEach(function(done){ 
    expect(auth.user.Session).toBe(74790760); 
    done(); 
}); 
+0

Eric,你好,首先感謝你的答案。因此,通過將done函數傳遞給我的'let's do this'規範來修改它,最終會出現一個令人困惑的錯誤: '錯誤 - 超時 - 異步回調未在jasmine.DEFAULT_TIMEOUT_INTERVAL指定的超時內調用。「 我沒有使用默認的超時intervall,而是我設置了2000毫秒,這應該覆蓋默認的超時intervall。甚至當我在setTimeout調用中除去'done()'調用的所有代碼時,都會發生這種情況 – DisconnectYourNeighbours

+1

現在它已經可以工作了,這要歸功於您的答案。好像我忘了在afterEach的末尾添加'done();'。謝謝 – DisconnectYourNeighbours

+2

它看起來並不像你的'afterEach'中有任何東西需要它本身是異步的。通過讓'it'爲異步,你的'afterEach'不會被調用,直到你在那裏調用'done'回調。你應該沒問題,完全從你的'afterEach'中刪除'done'參數。 – Gregg