TestRunner没有运行我的Mocha / Chai测试

时间:2014-08-22 15:44:51

标签: unit-testing requirejs mocha mocha-phantomjs

尽管我付出了最大的努力,但当我在浏览器中运行testRunner.html页面时,我似乎无法让我的testRunner.html确认我的测试。我已经确认它会拉入测试文件并运行expect,但测试运行器仍然说零通过而零失败。我还尝试将mocha.run()命令移动到testRunner.html页面作为内联脚本无效。

我的配置错误了什么?

testRunner.html

<!DOCTYPE html>
<html lang = "en">
    <head>
        <meta charset = "utf-8" />
        <title> 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 = "../node_modules/requirejs/require.js" data-main = "test.config.js"></script>
    </body>
</html>

test.config.js

require.config({
    baseUrl: '../src/public/js',
    paths: {
        jquery: '//code.jquery.com/jquery-2.1.1.min',
        chai: '/../../../node_modules/chai/chai',
        underscore: '//cdnjs.cloudflare.com/ajax/libs/underscore.js/1.6.0/underscore-min',
        backbone: '//cdnjs.cloudflare.com/ajax/libs/backbone.js/1.1.2/backbone-min',
        marionette: 'http://marionettejs.com/downloads/backbone.marionette',
        handlebars: '//cdnjs.cloudflare.com/ajax/libs/handlebars.js/1.3.0/handlebars',
        syphon: '//cdnjs.cloudflare.com/ajax/libs/backbone.syphon/0.4.1/backbone.syphon.min'
    },
    shim: {
        underscore: {
            exports: '_'
        },
        backbone: {
            deps: ['jquery', 'underscore'],
            exports: 'Backbone'
        },
        marionette: {
            deps: ['backbone'],
            exports: 'Marionette'
        },
        syphon: {
            deps: ['backbone', 'marionette'],
            exports: 'Backbone.Syphon'
        },
        handlebars: {
            exports: 'Handlebars'
        }
    }
});

require([
    '../../../test/src/appTest'
], function() {
    if (typeof mochaPhantomJS !== "undefined") {
        mochaPhantomJS.run();
    }
    else {
        mocha.run();
    }
});

appTest.js

define(['chai'], function(chai) {
    describe('array', function() {
        chai.expect(1+1).to.equal(2);
    });
});

1 个答案:

答案 0 :(得分:2)

您需要在it电话中进行测试:

define(['chai'], function(chai) {
    describe('array', function() {
        it("1 + 1 = 2", function () {
            chai.expect(1+1).to.equal(2);
        });
    });
});

这完全是您如何使用Mocha的问题。 RequireJS在这里根本不是一个因素。