2016-08-18 58 views
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調用不兼容的問題,但我無法弄清楚。

任何幫助表示讚賞!

+0

如果您懷疑PhantomJS是問題,您是否嘗試在真實瀏覽器中加載測試?這是我第一次嘗試。 – Louis

+0

謝謝,正如懷疑,它從瀏覽器正常工作,我想這是一個幻影問題 – Patrick

回答

0

該問題已通過向phantomJS添加配置選項得到解決。

這是用瑣碎的任務完成摩卡:

mocha : { 
     all : { 
      src : ['tests/testrunner.html'] 
     }, 
     options : { 
      run: true, 
      page: { 
       settings: { 
        webSecurityEnabled: false, // disable cors checks in phantomjs 
       }, 
      }, 
     }, 

注意webSecurityEnabled選項。