在Rails项目中使用Jasmine时,为了使Jasmine规范中的依赖项保持一致,我想像在实际页面上那样从cdn中提取jquery。我尝试在Jasmine.yml文件中这样做:

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

但是,这永远都行不通,因为当查看localhost:8888的源代码时,我得到:
<script src="/__spec__/http://code.jquery.com/jquery-1.9.1.js" type="text/javascript"></script>

您如何正确地做到这一点?

最佳答案

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);

09-26 20:39
查看更多