Requirejs 获取引用错误:Can';t find variable:描述尝试使用mocha和phantom js运行测试时的情况

Requirejs 获取引用错误:Can';t find variable:描述尝试使用mocha和phantom js运行测试时的情况,requirejs,phantomjs,mocha.js,durandal,Requirejs,Phantomjs,Mocha.js,Durandal,使用phantomJs使用mocha测试durandal应用程序时遇到问题。以下是我所做的: 首先,我创建了一个dummyPage,以包含requireJs环境中的测试: <!DOCTYPE html> <html> <head> <link rel="stylesheet" href="mocha.css" /> <script type="text/javascript" src="chai.js"></scr

使用phantomJs使用mocha测试durandal应用程序时遇到问题。以下是我所做的:

首先,我创建了一个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>
当我使用以下js文件运行phantomJs时,我得到了错误:

获取引用错误:找不到变量:descripe


在我看来,问题在于,在您开始要求包含测试的模块之前,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();
    });
};

谢谢你解决了这个问题。您还知道当我运行“phantomjs spec.js”时如何显示测试结果吗。目前我看不到任何outputI使用摩卡、柴和RequireJS,这足以回答您的问题,但我不使用PhantomJS,因此我无法帮助解决这个新问题。我建议检查PhantomJS的问题,以确保它还没有被回答,如果它没有被回答,然后问一个新的问题。
page.onLoadFinished = function () {
    page.evaluate(function (specFiles) {
        runTests(specFiles);
    }, specFiles);
};

page.open('mocha.htm');
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();
    });
};