ReferenceError:在yeoman中使用grunt测试时找不到变量x

时间:2014-06-08 14:50:48

标签: javascript unit-testing gruntjs mocha yeoman

我使用自耕农,咕噜,摩卡。我想做BDD,所以我进行基本的单元测试并在控制台中运行grunt test,这样我就可以在浏览器中使用ReferenceError: can't find variable: Creature打开test / index.html。

这是app / scripts中的creature.js文件:

'use strict';
var Creature = (function () {
    function Creature(name) {
        this.name = name;
    }
    Creature.prototype.sayHello = function (message) {
        return this.name + ' ' + message;
    };
    Creature.prototype.eat = function (item){
        return this.name + ' is eating ' + item;
    }
    return Creature;
})();

这是我的test / index.html

<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>Mocha Spec Runner</title>
    <link rel="stylesheet" href="bower_components/mocha/mocha.css">
</head>
<body>
    <div id="mocha"></div>
    <script src="bower_components/mocha/mocha.js"></script>
    <script>mocha.setup('bdd')</script>
    <script src="bower_components/chai/chai.js"></script>
    <script>
        var assert = chai.assert;
        var expect = chai.expect;
        var should = chai.should();
    </script>

    <!-- include source files here... -->
    <script src="../app/scripts/creature.js"></script>
    <!-- include spec files here... -->
    <script src="spec/test.js"></script>

    <script>mocha.run()</script>
</body>
</html>

这是我的test / spec / test.js文件:

/* global describe, it */

(function () {
    'use strict';

    describe('Creature', function () {
        describe('Comunnications', function () {
            it('should say <name> hi', function () {
                var creature = new Creature('test');
                expect(creature.sayHello('hi')).to.equal('test hi');
            });
            it('should say <name> is eating <item>', function () {
                var creature = new Creature('test');
                expect(creature.eat('pear')).to.equal('test is eating pear');
            });
        });
    });
})();

控制台日志:

Running "copy:styles" (copy) task

Done, without errors.

Running "autoprefixer:dist" (autoprefixer) task

Running "connect:test" (connect) task
Started connect web server on http://localhost:9001

Running "mocha:all" (mocha) task
Testing: http://localhost:9001/index.html

  ..

  0 passing (113ms)
  2 failing

  1) Creature Comunnications should say <name> hi:
     ReferenceError: Can't find variable: Creature
      at http://localhost:9001/spec/test.js:9
      at http://localhost:9001/bower_components/mocha/mocha.js:4263
      at http://localhost:9001/bower_components/mocha/mocha.js:4635
      at http://localhost:9001/bower_components/mocha/mocha.js:4694
      at next (http://localhost:9001/bower_components/mocha/mocha.js:4561)
      at http://localhost:9001/bower_components/mocha/mocha.js:4570
      at next (http://localhost:9001/bower_components/mocha/mocha.js:4514)
      at http://localhost:9001/bower_components/mocha/mocha.js:4538
      at timeslice (http://localhost:9001/bower_components/mocha/mocha.js:5531)

  2) Creature Comunnications should say <name> is eating <item>:
     ReferenceError: Can't find variable: Creature
      at http://localhost:9001/spec/test.js:13
      at http://localhost:9001/bower_components/mocha/mocha.js:4263
      at http://localhost:9001/bower_components/mocha/mocha.js:4635
      at http://localhost:9001/bower_components/mocha/mocha.js:4694
      at next (http://localhost:9001/bower_components/mocha/mocha.js:4561)
      at http://localhost:9001/bower_components/mocha/mocha.js:4570
      at next (http://localhost:9001/bower_components/mocha/mocha.js:4514)
      at http://localhost:9001/bower_components/mocha/mocha.js:4538
      at timeslice (http://localhost:9001/bower_components/mocha/mocha.js:5531)


>> 2/2 tests failed (0.11s)
Warning: Task "mocha:all" failed. Use --force to continue.

Aborted due to warnings.


Execution Time (2014-06-08 14:40:23 UTC)
concurrent:test     3s  ■■■■■■■■■■■■■■■ 32%
connect:test     447ms  ■■■ 5%
mocha:all         5.9s  ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ 63%
Total 9.4s

也许它与./app/scripts/creature.js的引用有关,当通过phantomjs运行它与在真实浏览器中使用它时有不同的路径?

修改

似乎grunt test打开了错误的测试路径。它似乎打开root / index.html当它打开时...我通过改变creature.js的路径(删除 app / )来实现它

<!-- include source files here... -->
<script src="../scripts/creature.js"></script>

但是,当运行grunt test时,它现在可以正常工作,但是现在打开test / index.html文件会在firebug控制台中显示错误ReferenceError: Creature is not defined

我做错了什么或者这是正确的行为吗?我可以做些什么来使它同时发挥作用?

我通过更改gruntfile.js更改的测试来发现问题所在:options:open to true。

// The actual grunt server settings


connect: {
        options: {
            port: 9000,
            open: true,
            livereload: 35729,
            // Change this to '0.0.0.0' to access the server from outside
            hostname: 'localhost'
        },
        livereload: {
            options: {
                ...
            }
        },
        test: {
            options: {
                open: false,
                port: 9001,
                middleware: function(connect) {
                    return [
                        connect.static('.tmp'),
                        connect.static('test'),
                        connect().use('/bower_components', connect.static('./bower_components')),
                        connect.static(config.app)
                    ];
                }
            }
        },
        dist: {
            ...
            }
        }
    }

提前致谢!

可能与Running Mocha on the command line and Including a file

有关

3 个答案:

答案 0 :(得分:0)

这可能是因为这条线。 connect.static(config.app)。在命令行上运行它,它运行connect,并将root / base路径(它读取文件的位置)设置为app并进行测试。因此,如果您删除应用程序,它适用于命令行,但如果您将其作为文件运行,它是相对的,因此需要&#39; app&#39;

答案 1 :(得分:-1)

可能你打电话

<script>mocha.run()</script>

当DOM尚未准备好时 试试这个

<script>
if( document.readyState === "complete" ) {
   var creature = new Creature('test'); 
   alert(creature .name);
   //mocha.run()
}
</script>

或使用jQuery

$(function() {
var creature = new Creature('test'); 
   alert(creature .name);
   //mocha.run()
});

答案 2 :(得分:-1)

你有没有尝试过

<script src="../app/scripts/creature.js"></script>

<script src="bower_components/mocha/mocha.js"></script>

看起来var没有被定义,可能是volkinc在回应之后的意思。