2014-01-14 53 views
2

我有一個測試方法,如果我通過SpecRunner運行它就會通過,但如果從命令行運行測試將會失敗(如咕嚕咕嚕)。茉莉花異步測試通過SpecRunner但通過命令行失敗(grunt-contrib-jasmine)

測試如下所示。 doSomething()方法進行AJAX調用,並使用響應在ID =「my-test」的DIV內創建UL元素。

describe('my AJAX method', function() { 
beforeEach(function() { 
    this.$myTest = $('<div id="my-test"></div>'); 
    this.$myTest.appendTo($('body')); 
}); 
afterEach(function() { 
    this.$myTest.remove(); 
}); 
it('should create a UL from the result of the Ajax request', function() { 
    runs(function() { 
    myObj.doSomething(); // this makes an AJAX request and builds a UL from the response 
    }); 
    waitsFor(function() { 
    return(this.$myTest.find('ul').length > 0); 
    }, 'UL has been created', 2000); 
    runs(function() { 
    expect(this.$myTest.find('ul').length).toEqual(1); 
    }); 
}); 
}); 

在命令行中的錯誤信息是: -

Running "jasmine:src" (jasmine) task 
Testing jasmine specs via phantom 
............x 
my AJAX method:: should create a UL from the result of the Ajax request: failed 
timeout: timed out after 2000 msec waiting for UL has been created (1) 
13 specs in 1.749s. 
>> 1 failures 
Warning: Task "jasmine:src" failed. Use --force to continue. 

Aborted due to warnings. 

沒有人有任何想法,爲什麼這可能發生?

+0

添加有關您遇到的問題的更多詳細信息(錯誤消息,堆棧跟蹤等)。請參閱規則(http://stackoverflow.com/questions/how-to-ask) – uthark

回答

0

我發現使用grunt-contrib-jasmine時不會立即創建body元素,因此您的#my-test div可能不會附加到body元素。你可以試試

$('body').ready(function { 
    // Do stuff with body. 
}); 

如果你使用的是jQuery。

老問題,但也許它可以幫助別人搜索。