我在使用phantomJs使用mocha测试我的durandal应用程序时遇到了问题。这是我做的:
Firt我创建了一个dummyPage来包含requireJs环境下的测试:
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="mocha.css" />
<script type="text/javascript" src="chai.js"></script>
<script type="text/javascript" src="mocha.js"></script>
<script type="text/javascript" src="../lib/jquery/jquery-1.9.1.min.js"></script>
<script type="text/javascript" src="../lib/knockout/knockout-2.3.0.js"></script>
<script type="text/javascript" src="../lib/require/require.js"></script>
<script type="text/javascript">
require.config({
baseUrl: '../app/',
paths: {
'app': '../app',
'specs': '../sampleTest/specs/',
'text': '../lib/require/text',
'durandal': '../lib/durandal/js',
'plugins' : '../lib/durandal/js/plugins',
'transitions' : '/lib/durandal/js/transitions',
'knockout': '../lib/knockout/knockout-2.3.0',
'jquery': '../lib/jquery/jquery-1.9.1'
}
});
var runTests = function (specfiles) {
console.log('enter runTests');
require(specfiles, function () {
mocha.setup('bdd');
assert = chai.assert;
mocha.run();
});
};
</script>
</head>
<body>
</body>
</html>
我的dummyTest看起来像那样:
console.log('输入虚拟测试');
define(['viewmodels/flickr'], function (flickr) {
describe('Test 1', function(){
it('should append a value', function () {
var arr = [];
arr.push('foo');
arr.push('bar');
assert.equal(arr[0],'foo', 'foo is a string');
});
});
})
当我使用以下js文件运行phantomJs时,我收到错误:
获取ReferenceError:找不到变量:describe
page.onLoadFinished = function () {
page.evaluate(function (specFiles) {
runTests(specFiles);
}, specFiles);
};
page.open('mocha.htm');
答案 0 :(得分:1)
在我看来,问题是在开始要求包含测试的模块之前,Mocha没有正确初始化。更改您的runTests
功能,以便在加载之前执行测试模块所需的初始化。像这样:
var runTests = function (specfiles) {
console.log('enter runTests');
// Initialize mocha and leak assert into the global space.
mocha.setup('bdd');
assert = chai.assert;
require(specfiles, function () {
mocha.run();
});
};