ReferenceError:在yeoman中使用grunt测试时找不到变量x [英] ReferenceError: can't find variable x when using grunt test in yeoman

查看:104
本文介绍了ReferenceError:在yeoman中使用grunt测试时找不到变量x的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用yeoman,grunt,mocha。我想做BDD,所以我进行基本的单元测试,并在控制台中运行 grunt test ,这给我 ReferenceError:找不到变量: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-Compatiblecontent =IE = edge>
< title>摩卡Spec Runner< / title>
< link rel =stylesheethref =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>

<! - 包括此处的源文件... - >
< script src =../ app / scripts / creature.js>< / script>
<! - 在这里包含规格文件... - >
< script src =spec / test.js>< / script>

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

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

  / *全局描述,它* / 

(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 ('应该说< name>正在吃< item>',function(){
var creature = new Creature('test');
expect(creature.eat('pear')) .to.equal('test is eating pear');
});
});
});
})();

控制台日志:

 运行copy:styles(复制)任务

完成,没有错误。

运行autoprefixer:dist(autoprefixer)任务

运行connect:test(连接)任务
开始在http:// localhost上连接Web服务器:9001

运行mocha:all(mocha)任务
测试:http:// localhost:9001 / index.html

..

0传球(113ms)
2失败

1)生物传播应该说< name>嗨:
ReferenceError:无法找到变量:http:// localhost:9001 / spec / test.js处的生物
:9
位于http:// localhost:9001 / bower_components / mocha / mocha.js:4263
,位于http:// localhost:9001 / bower_components / mocha / mocha.js:4635
位于http:// localhost:9001 / bower_components / mocha / mocha.js: $ http:// localhost:9001 / bower_components / mocha / mocha.js:4570
$ b(http:// localhost:9001 / bower_components / mocha / mocha.js:4561) $ b at next(http:// localhost:9001 / bower_components / mocha / mocha.js:4514)
在http:// localhost:9001 / bower_components / mocha / mocha.js:4538
at timeslice(http:// localhost:9001 / bower_components / mocha / mocha.js:5531)

2)Creature Comunnications应该说< name>正在吃< item> ;:
ReferenceError:在http:// localhost:9001 / spec / test.js:13
找不到变量:Creature
:http:// localhost:9001 / bower_components / mocha / mocha.js:4635
at http:// localhost:9001 / bower_components / mocha在http:// localhost:9001 / bower_components / mocha / mocha下的/mocha.js:4694
(http:// localhost:9001 / bower_components / mocha / mocha.js:4561)
。 $ http:// localhost:9001 / bower_components / mocha / mocha.js:4538
(http:// localhost:9001 / bower_components / mocha / mocha.js:4514)
在时间片(http:// localhost:9001 / bower_components / mocha / mocha.js:5531)


>> 2/2测试失败(0.11s)
警告:任务摩卡:全部失败。使用--force继续。

由于警告而中止。


执行时间(2014-06-08 14:40:23 UTC)
并发:测试3s■■■■■■■■■■■■■■■■■ 32%
连接:测试447ms■■■5%
摩卡:全部5.9s■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ ■■■63%
总计9.4s

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



编辑



看起来像 grunt test 会打开错误的路径进行测试。它似乎打开根/ index.html当它打开时...我通过更改到creature.js的路径(删除了应用程序/

 <! - 此处包含源文件...  - > 
< script src =../ scripts / creature.js>< / script>

然而,当运行 grunt test ,但打开test / index.html文件它现在给我的错误 ReferenceError:生物未定义在萤幕控制台。



我做错了什么或者是否有正确的行为?我能做些什么来使它成为双向工作?



我通过更改gruntfile.js更改了test:options:open to true 。

  //实际的grunt服务器设置


connect:{
选项:{
port:9000,
open:true,
livereload:35729,
//将其更改为'0.0.0.0'以从$ b之外访问服务器$ b主机名:'localhost'
},
livereload:{
选项:{
...
}
},
测试:{
options:{
open:false,$ b $ port:9001,
middleware:function(connect){
return [
connect.static( '.tmp'),
connect.static('test'),
connect()。use('/ b ower_components',connect.static('./ bower_components')),
connect.static(config.app)
];
}
}
},
dist:{
...
}
}
}

预先感谢!



可能与在命令行上运行Mocha并包含文件 p>

解决方案

这可能是因为这一行。 connect.static(config.app)。在命令行上运行它,它运行连接,并将根/基路径(它读取文件的位置)设置为应用程序并进行测试。所以,如果你删除应用程序它的命令行工作,但是如果你运行它作为一个文件,它是相对的,因此需要'应用程序'


I'm using yeoman, grunt, mocha. I want to do BDD so I make basic unit test and run grunt test in console, which gives me ReferenceError: can't find variable: Creature opening test/index.html in browser works as it should.

This is my creature.js file in app/scripts:

'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;
})();

This is my 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>

This is my test/spec/test.js file:

/* 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');
            });
        });
    });
})();

Console log:

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

Perhaps it has to do with the reference to ../app/scripts/creature.js when running through phantomjs it has a different path compared to using it in a real browser?

EDIT

It seems like grunt test opens the wrong path to test on. It seems to open root/index.html When it opens... I got it to work by changing the path to the creature.js (removed the app/)

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

However when running grunt test it now works, but opening the test/index.html file it now gives me the error ReferenceError: Creature is not defined in firebug console.

Do I do something wrong or is that de correct behaviour? Anything I can do to make it work both ways?

I discovered where the issue was by changing the gruntfile.js changed test: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: {
            ...
            }
        }
    }

Thanks in advance!

Possibly related to Running Mocha on the command line and Including a file

解决方案

This is probably because of this line. connect.static(config.app). Running it on the commandline, it runs connect, and sets the root/base path (where it reads the file) to app and test. So if you remove app it works for commandline, however if you are running it as a file, it is relative, hence needing 'app'

这篇关于ReferenceError:在yeoman中使用grunt测试时找不到变量x的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆