Mocha在浏览器中传递测试,在命令行上失败

时间:2014-06-06 16:57:06

标签: testing gruntjs phantomjs mocha chai

我正在使用mocha-grunt能够在命令行上运行/查看我的客户端测试。出于某种原因,当我在命令行上运行测试时,似乎会调用decrementColumn两次。

我想知道是否有某种phantomJS窗口范围问题与beforeEach有关,因为如果我将该代码块(新的Level实例化)复制到decrementColumn测试的顶部,那么一切都适用于浏览器和命令行。

功能

decrementColumn: function( columnPosition ){
    --this.columns[ 0 ]
},

TEST

describe( 'Level', function(){
    beforeEach( function(){
        level = new Level( [ 2, 3, 5 ] )
    })
describe( '#decrementColumn()', function(){
    it( 'should reduce column by one', function(){
        level.decrementColumn( 1 )
        expect ( level.columns[ 0 ] ).to.equal( 1 )
    })
})

})

GRUNTFILE

module.exports = function( grunt ){
    grunt.initConfig({
        pkg: grunt.file.readJSON( 'package.json' ),
        mocha: {
            all: {
                src: [ 'test/runner.html' ],
            },
            options: {
                reporter: 'Spec',
                run: true
            }
        }
    });

    grunt.loadNpmTasks( 'grunt-mocha' )

    grunt.registerTask( 'default', [ 'mocha' ] )
};

SPECRUNNER

<html>
<head>
    <meta charset="utf-8">
    <title>Mocha Tests</title>
    <link rel="stylesheet" href="../lib/mocha.css" />
</head>
<body>
    <div id="mocha"></div>
    <!-- mocha dependencies -->
    <script type="text/javascript" src="../lib/jquery.min.js"></script>
    <script type="text/javascript" src="../lib/expect.js"></script>
    <script type="text/javascript" src="../lib/mocha.js"></script>
    <script type="text/javascript" src="../lib/chai.js"></script>

    <!-- application files -->
    <script type="text/javascript" src='../client/view.js'></script>
    <script type="text/javascript" src='../client/logic.js'></script>
    <script type="text/javascript" src='../client/level.js'></script>
    <script type="text/javascript" src='../client/game.js'></script>
    <script type="text/javascript" src='../client/init.js'></script>

    <!-- configure mocha && chai -->
    <script>
        mocha.setup( 'bdd' )
        expect = chai.expect
    </script>

    <!-- test files -->
    <script type="text/javascript" src='level-spec.js'></script>

    <script>
        mocha.globals(['jQuery']);
        mocha.run();
    </script>
</body>
</html>

browser command line

1 个答案:

答案 0 :(得分:3)

您是否关注本指南? https://gist.github.com/maicki/7781943

您的测试运行器文件是什么样的?你可能错过了一条线......

<script>
    // Only tests run in real browser, injected script run if options.run == true
    if (navigator.userAgent.indexOf('PhantomJS') < 0) {
      mocha.run();
    }
</script>