2013-02-18 30 views
5

在Rails項目中使用Jasmine時,爲了保持Jasmine規範中的依賴性一致,我想從實際頁面上的cdn中獲取jquery。我嘗試這樣做,像這樣,在我Jasmine.yml文件:如何在Jasmine中包含來自CDN的JavaScript文件?

helpers: 
    - http://code.jquery.com/jquery-1.9.1.js 

但是,這永遠不會奏效,查看本地主機的來源時爲:8888我得到:

<script src="/__spec__/http://code.jquery.com/jquery-1.9.1.js" type="text/javascript"></script> 

怎麼辦你這個正確嗎?

回答

4

https://github.com/pivotal/jasmine-gem/issues/135

回答我寫了一個幫手來加載外部JavaScript的。這不是最好的解決方案(我更喜歡使用配置文件),但它適用於我。

var head = document.getElementsByTagName('head')[0]; 
var jQueryScript = document.createElement('script'); 
jQueryScript.setAttribute('type', 'text/javascript'); 
jQueryScript.setAttribute('src', '//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js'); 
head.appendChild(jQueryScript); 
1

@ emrox的答案是正確的,適用於茉莉花2.0。

然而在茉莉花2.1.3它沒有。這是因爲當測試運行時(我使用Chutzpah)「http:」未被預定爲CDN參考。它在茉莉花2.0版本。

我實現的起作用的涉及以下代碼行的修復:

jQueryScript.setAttribute( 'SRC', 'http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js');

希望這可以幫助別人在他們決定降級到2.0之前!

0

問題135使它成爲你可以列出你的jasmine.yml文件中的CDN條目。例如

src_files: 
    - http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.js 
    - http://ajax.googleapis.com/ajax/libs/jqueryui/1.11.2/jquery-ui.min.js 
    - lib/javascript/**/*.js 
相關問題