[英]Why is Jasmine not executing it() on this async test?
我正在嘗試測試一種原型方法,該方法返回有關通過AJAX加載的數據集的見解。
$.getJSON('../data/bryce.json').done(function(data) {
insights = new Insights(data);
describe("People Method", function() {
console.log('it executes this far');
it("should return complete people data", function() {
console.log('but not this far');
expect(insights.people()).toBeTruthy();
});
});
});
當我運行此測試套件時,describe()會執行,但it()不會。 一般來說,我對JavaScript測試還很陌生,所以我想我做錯了什么。 但我不確定這是什么。
另外,由於我正在使用的數據是一個巨大的 JSON文件,因此實際上不可能將其包含在此文件中。 甚至不可能提供樣本大小的版本。 數據集中的每個對象長數百行。
Jasmine設計了一種排隊機制,並執行所有describe
和it
功能來排隊要執行的工作。
在Jasmine中異步進行工作需要您遵循一定的模式。
茉莉花1.x
describe('some suite', function(){
it('some test', function(){
var data;
//Execute some async operation
runs(function(){
$.get('myurl').done(function(d){ data = d; });
});
//Wait for it to finish
waitsFor(function(){
return typeof data !== 'undefined';
});
//Assert once finished
runs(function(){
expect(data.foo).toBe('bar');
});
});
});
茉莉花1.x中使用一種特殊的輪詢機制,以保持輪詢waitsFor
方法,直到超時,或返回true,然后執行最終的runs
方法。
茉莉花2.x
describe('some suite', function(){
var data;
beforeEach(function(done){
$.get('myurl').done(function(d){
data = d;
//Signal test to start
done();
});
});
it('some test', function(done){
expect(data.foo).toBe('bar');
//Signal test is finished
done();
});
});
Jasmine 2.x有所不同,因為它使用信號機制來指示何時開始和完成測試。 您的規格可以采用可選的done
方法來用於同步測試。
如果您在beforeEach
使用done
方法,那么直到調用該方法,它才會開始測試。
如果您在it
函數中使用了done
方法,那么直到調用該方法,測試才會結束。
這兩個都可用於有效管理測試中的異步行為。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.