PhantomJS应用程序接口声称允许通过标准的commonJS接口访问'fs‘和其他一些内置的require模块。grunt-contrib jasmine声称可以使用phantomJS运行所有规范。但是当我使用grunt-contrib-jasmine时,require方法似乎不可用?
fs = require('fs')
describe 'DesignService', ->
it 'test loadFromJSON', ->
jsonFile = fs.read("resources/sample_pole.json")给了我一个错误:
ReferenceError: Can't find variable: require at
>> target/spec/Spec.js:3 我做错了什么?
如果不清楚,我从coffeescript编译,然后将grunt-contrib-jasmine指向编译的输出。其他的规格都运行得很好。
发布于 2013-07-23 12:53:24
原因
require方法只在服务器端可用(Nodejs/PhantomJS),但所有的jasmine测试(specs)都在客户端执行。
可能的解决方案
您可以在helpers文件夹中创建一个JavaScript文件,其内容如下:
window.jsonFile = { some : json_object }并在等级库文件中使用jsonFile参考。
解释
在PhantomJS描述中:
PhantomJS是一个带有JavaScript应用编程接口的无头WebKit脚本。
通过PhantomJS无意识地运行jasmine规范。
grunt-contrib-jasmine会自动创建包含所有用户规格的_SpecRunner.html文件(例如,见下文),并将其传递给PhantomJS。PhantomJS是一个单独的可执行文件,它在Nodejs中只被包装为一个包。这是与从Phantomjs.org页面下载的相同的可执行文件。
最后,执行以下代码行:.\node_modules\grunt-contrib-jasmine\node_modules\grunt-lib-phantomjs\node_modules\phantomjs\lib\phantom\phantomjs .\node_modules\grunt-contrib-jasmine\node_modules\grunt-lib-phantomjs\phantomjs\main.js .\_SpecRunner.html。在这里,main.js文件用于打开页面并绑定抛出到grunt日志记录的警报(alert(jsonString))。
因此,在** _SpecRunner.html 和PhantomJS规范文件中没有提供和jasmine规范文件。
结果与用浏览器打开_SpecRunner.html是一样的,只是所有的消息都会被jasmine reporter截获并显示在屏幕上。
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Jasmine Spec Runner</title>
<link rel="stylesheet" type="text/css" href=".grunt/grunt-contrib-jasmine/jasmine.css">
<!-- Jasmine test suite -->
<script src="./.grunt/grunt-contrib-jasmine/jasmine.js"></script>
<script src="./.grunt/grunt-contrib-jasmine/jasmine-html.js"></script>
<!-- Some vendor libraries -->
<script src="./test/vendor/jquery.js"></script>
<!-- Some helpers -->
<script src="./test/helpers/ts.js"></script>
<!-- Your spec files -->
<script src="./test/main_spec.js"></script>
<!-- Jasmine reporter that displays the result-->
<script src="./.grunt/grunt-contrib-jasmine/reporter.js"></script>
<script src="./.grunt/grunt-contrib-jasmine/jasmine-helper.js"></script>
</head>
<body>
</body>
</html>https://stackoverflow.com/questions/17677654
复制相似问题