Tag: 摩卡

unit testing在Nodejs中发出的事件的最好方法是什么?

我正在写一堆摩卡testing,我想testing一下发生的特定事件。 目前,我正在这样做: it('should emit an some_event', function(done){ myObj.on('some_event',function(){ assert(true); done(); }); }); 但是,如果这个事件从不发生,它会使testing套件崩溃,而不是失败那个testing。 什么是testing这个最好的方法?

获取TypeError:这不是在mocha中使用Buffer.from的types数组

我正在使用Mocha / Chai来unit testing一个最近开始使用nodejs的Buffer对象来解决不同的问题的库。 我在unit testing中得到这个错误信息: TypeError: this is not a typed array. at Function.from (native) at Object.hashesMatch (index.js:29:18 at Context.<anonymous> (test/test.js:25:22) index.js第29行是我使用nodejs的缓冲区… var b = Buffer.from ('some string or other'); 我无法find一个polyfill或解决方法,所以将不胜感激的build议。 谢谢

摩卡beforeEach vs之前执行

最近我碰到一个我无法解释的问题。 在这些testing中我有很多代码,所以我会尽我所能来捕捉这个主意 我有这样的testing: describe('main page', function(){ beforeEach(function(done){ addUserToMongoDb(done); // #1 }); afterEach(function(done){ removeUserFromMongoDb(done); }); context('login', function(){ it('should log the user in, function(){ logUserIn(user_email); // #2 – This line requires the user from the beforeEach }); }); context('preferences', function(){ before(function(done){ //#3 logUserInBeforeTest(user_email); }); it('should show the preferences', function(){ doCheckPreferences(); // #4 }); }); }); 问题是, #1运行良好。 […]

在使用摩卡和伊斯坦布尔时不包括覆盖范围内的文件

如何使用摩卡和instanbul从覆盖率报告中排除文件夹和文件(按path)? 我想排除一个configuration,而不是 /*istanbul ignore next*/ 在每个文件中。 (Jenkins生成的报告使用) 谢谢,

使用mocha-phantomjs来自动化functiontesting

我的项目使用:Node,Coffeescript,SocketIO,Browserify和Mocha。 (用于标准服务器端unit testing的mocha) 我想用一个无头浏览器自动化一些客户端接口testing。 PhantomJS看起来像是理想的select(由于networking套接字的支持而select僵尸)。 PhantomJS页面警告说,这不是一个testing运行者,据我所知,他们build议使用mocha-phantomjs项目来驱动你的testing。 所以我已经能够运行示例testing(例如mocha-phantomjs tests/mixed.html ),但是我目前的问题实际上是在testing中使用PHANTOM。 所有mocha-phantomjs回购的样本testing似乎都使用标准的mocha服务器端unit testing。 例如,我可以轻松运行mocha-phantomjs tests/mixed.html来查看枯燥的旧unit testing。 或者我可以运行phantomjs tests/login.coffee来加载我的login屏幕…但是我怎么phantomjs tests/login.coffee两个结合起来,对我在login屏幕上看到的内容做出断言? 我在网上找不到任何这样的例子,我正在努力理解这个最好的方法。 希望这一切都有道理。 预先感谢您的帮助。 更新 :我发现作者( 这里 )的以下build议,但我真的不明白如何处理它: phantomjs lib/mocha-phantomjs.coffee test/mixed.html

Chai与should.js与Mocha为Node.js

我看到使用Chai的文章,但是should.js来自Express和Mocha的制作人TJ Holowaychuk,这是一个很好的谱系。 我应该使用Chai还是使用Mocha的should.js来创build一个仅用于Node.js服务器端的新项目?

如何运行在TypeScript中编写的Mochatesting?

安装程序:我有一个用TypeScript编写的节点项目(纯节点,没有浏览器位)。 我可以使用typescript模块中的TypeScript编译器( tsc )编译代码。 到现在为止还挺好。 不过,我想用摩卡书写testing,这就是我遇到麻烦的地方。 我试过 – –compilers ts:typescript ,但我不断收到像这样的错误: error TS5023: Unknown compiler option 'compilers'. 它看起来像mocha的命令行结束被传递给tsc ,这显然是不好的。

模拟/testingMongodb数据库Node.js

我正在学习nodejs,我有一个mongodb数据库,我必须与之交互。 我目前正在考虑使用mocha作为unit testing框架,zombie.js作为验收testing框架。 我想知道我怎么能做全面的接受testing,哪些击中了mongodb数据库。 有没有一个框架/模块可以帮助用testing数据库replace数据库,或者mocha或zombie.js是否具有可以轻松替代数据库的function。 在创build数据库对象时,还有一个类似于工厂(而不是灯具)的框架。 我在rails领域遇到的一个类似的概念是在rspec中,有一个spec_helper.rb文件,它在testing运行之前运行,它设置项目configuration以决定运行testing时要打哪个数据库。 在testing运行之前,它使用database_cleaner清理testing数据库。 对于工厂来说,我曾经使用过Factory Girl来再次在rails世界中从数据库模式创build工厂对象。 谢谢

Supertest,我可以创build一个替代的请求与默认设置一些标头?

我正在用Mocha的Supertest来testing用Node JS开发的API。 我想在API上做很多不同的testing。 几乎所有的人都必须重新设置Authorization和Content-Type头(因为API需要这个testing)。 it('Creation without an email address should fail and return error code 50040', function(done) { request .post('/mpl/entities') .set('Authorization', 'Token 1234567890') //set header for this test .set('Content-Type', 'application/json') //set header for this test .send({ firstname: "test" }) .expect('Content-Type', /json/) .expect(500) .expect(anErrorCode('50040')) .end(done); }); it('Creation with a duplicate email address should fail and return […]

我如何让Mocha加载一个helper.js文件来定义全局钩子或者实用程序?

我有一个名为test/helper.js的文件,用于在我的Node.js应用程序上运行Mochatesting。 我的testing结构如下所示: test/ test/helper.js # global before/after test/api/sometest.spec.js test/models/somemodel.spec.js … more here helper.js文件必须被加载,因为它包含我的testing套件的全局钩子。 当我运行摩卡来执行整个testing套件时,像这样: mocha –recursive test/ helper.js文件在我的testingbefore加载,并且我的before钩子按预期执行。 但是,当我只运行一个特定的testing时, helper.js不会在testing之前加载。 这是我如何运行它: mocha test/api/sometest.spec.js 没有全球调用before ,甚至没有一个console.log('I WAS HERE'); 。 那么如何让Mocha 始终加载我的helper.js文件呢?