0
我試圖掛鉤grunt-mocha與requireJS做一些單元測試,它工作的很好,除了我似乎無法測試ajax調用。
我已經得到的代碼回購了起來:https://bitbucket.org/IamHttP/grunt-mocha-tests/overview
特定的文件可以發現here
代碼本身是粘貼在這裏:
describe('testJqueryAjax', function(){
it('fetch data from an xhr', function(done){
this.timeout(3000);
require(['jquery'],function($){
$.ajax({
url: 'https://api.github.com',
success: function(str){
try{
chai.assert.equal(1 , 1);
done();
}
catch(e){
done(e);
}
}
}).fail(function(){
try{
chai.assert.ok(false);
done();
}
catch(e){
done(e);
}
});
});
});
});
問題:
ajax請求總是失敗,無論我指向它。總是調用.fail
函數。
我猜這是phantomJS與jQuery的$ .ajax調用不兼容的問題,但我無法弄清楚。
任何幫助表示讚賞!
如果您懷疑PhantomJS是問題,您是否嘗試在真實瀏覽器中加載測試?這是我第一次嘗試。 – Louis
謝謝,正如懷疑,它從瀏覽器正常工作,我想這是一個幻影問題 – Patrick