2015-11-18 26 views
0

我有一個簡單的庫,要求未能在PhantomJs測試

-- src/lib.js -- 
function libtest() { 
    return 2; 
} 

,我想測試一下,這是我收集需要PhantomJs。我有一個測試文件:

-- test/lib.test.js -- 
var fs = require('fs'); 
var assert = require('assert'); 
describe('Test module', function() { 
    var s = fs.readFileSync('../src/lib.js', 'utf8'); 
    eval(s); 
    it('Shows name', function() { 
     assert.equal(libtest(), 2); 
    }); 
}); 

,我準備爲瀏覽器:

# browserify lib.test.js -o tests.js 

,然後包括在我的主頁:

-- test/index.html -- 
<html> 
<head> 
    <meta charset="utf-8"> 
    <title>Mocha Tests</title> 
    <link href="../../node_modules/mocha/mocha.css" rel="stylesheet" /> 
</head> 
<body> 
    <div id="mocha"></div> 
    <script src="../../node_modules/mocha//mocha.js"></script> 
    <script>mocha.setup('bdd')</script> 
    <script src="tests.js"></script> 
    <script> 
    if (window.mochaPhantomJS) mochaPhantomJS.run(); 
    else mocha.run(); 
    </script> 
</body> 
</html> 
然而

,當我運行測試:

# grunt test 

我得到fo llowing錯誤:

Running "mocha_phantomjs:all" (mocha_phantomjs) task TypeError: 'undefined' is not a function (evaluating 'fs.readFileSync('../src/lib.js', 'utf8')')

at file:///Users/ekkis/Development/tst/test/tests.js:5
at file:///Users/ekkis/Development/tst/node_modules/mocha//mocha.js:529
at file:///Users/ekkis/Development/tst/test/browser/tests.js:10 in s at file:///Users/ekkis/Development/tst/test/browser/tests.js:1 in e at file:///Users/ekkis/Development/tst/test/browser/tests.js:1 at file:///Users/ekkis/Development/tst/test/browser/tests.js:1090 Unsafe JavaScript attempt to access frame with URL about:blank from frame with URL file:///Users/ekkis/Development/tst/node_modules/mocha-phantomjs-core/mocha-phantomjs-core.js. Domains, protocols and ports must match.

,這似乎是在抱怨fs是不確定的。然而,如果require('assert')工作,我想require('fs')應該工作。但我並不完全瞭解運行的環境。

我錯過了什麼?

回答

0

好的。答案僅僅是將庫包含在.html中。由於測試在瀏覽器中運行,因此該庫將可用。像這樣:

<body> 
    <div id="mocha"></div> 
    <script src="../../node_modules/mocha//mocha.js"></script> 
    <script>mocha.setup('bdd')</script> 
    <script src="../../src/lib.js"></script> 
    <script src="tests.js"></script>