6

PhantomJS API は、標準の require インターフェイスを介して「fs」およびその他のいくつかの組み込み commonJS モジュールへのアクセスを許可すると主張しています。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 をコンパイルの出力に向けます。その他のスペックは問題なく動いています。

4

1 に答える 1

6

原因

requireメソッドはサーバー側 (Nodejs/PhantomJS) でのみ使用できますが、すべてのジャスミン テスト (仕様) はクライアント側で実行されます。

考えられる解決策

次のような内容の JavaScript ファイルをhelpersフォルダに作成できます。

window.jsonFile = { some : json_object }

そしてjsonFile、スペックファイルで参照を使用してください。

説明

PhantomJSの説明から:

PhantomJS は、JavaScript API でスクリプト可能なヘッドレス WebKit です。

grunt-contrib-jasmine の説明から:

PhantomJS を介してジャスミン仕様をヘッドレスで実行します。

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ファイルは、ページを開きalert(jsonString)、grunt ロギングにスローされるアラート ( ) をバインドします。

そのため、PhantomJS API は で使用できますが、および jasmine 仕様ファイルでは使用できmain.jsません。_SpecRunner.html

結果はブラウザで開いた場合と同じですが_SpecRunner.html、すべてのメッセージが jasmine レポーターによって傍受され、画面に表示されます。

<!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>
于 2013-07-23T04:53:23.680 に答える