2015-11-18 28 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>