我想在node.js中使用mocha。我使用的最后一个测试框架是来自RubyonRails的Rspec,所以我尝试以相同的方式进行测试,但我被huge框架和我可以使用的所有库所迷惑。我正在关注官方的开始,但它没有解释如何组织测试。http://visionmedia.github.io/mocha/#installation现在,我了解到我可以使用以下库:https://github.com/visionmedia/should.js-用于测试模型实例的东西。https://github.com/LearnBoost/expect.js-基于should.js的简约BDD断言工具包ht
我有一组gulp.js运行我的mocha测试的目标,这些测试就像运行gulp-mocha的魅力一样。.问题:如何调试通过gulp运行的mocha测试?我想使用node-inspector之类的东西在我的src和测试文件中设置断点以查看发生了什么。我已经可以通过直接调用node来完成此操作:node--debug-brknode_modules/gulp/bin/gulp.jstest但我更喜欢为我包装这个的gulp目标,例如:gulp.task('test-debug','Rununittestsindebugmode',function(cb){//todo?});想法?我想避免使用
当我使用Mocha进行测试时,我经常需要同时运行异步和同步测试。Mocha很好地处理了这个问题,让我可以在我的测试异步时指定回调,done。我的问题是,Mocha如何在内部观察我的测试并知道它应该等待异步事件?只要我在测试函数中定义了回调参数,它似乎就在等待。您可以在下面的示例中看到,第一个测试应该超时,第二个应该在user.save调用匿名函数之前继续并完成。//Inanasynctestthatdoesn'tcalldone,mochawilltimeout.describe('User',function(){describe('#save()',function(){it('s
我想确保如果代码在测试模式下运行,它不会(意外)访问错误的数据库。检测代码当前是否在测试模式下运行的最佳方法是什么? 最佳答案 正如评论中已经提到的,在构建代码时了解测试是不好的做法。我什至在SO甚至外面都找不到提到的话题。但是,我可以想办法检测在测试中启动的事实。对我来说mocha并没有将自己添加到global范围内,而是添加了global.it。所以你的支票可能是varisInTest=typeofglobal.it==='function';我建议确保您不要误检以检查您最有可能在您的Node中使用的global.sinon和g
我有一些mocha测试需要来自先前函数调用的数据。但是,由于我的代码使用的是Web服务,因此我希望它在运行下一个测试之前等待一段预定的时间。类似这样的:varglobal;it('shouldgivesomeinfo',function(done){run.someMethod(param,function(err,result){global=result.globaldone();});});wait(30000);//basicallyblockitfromrunningthenextassertionit('shouldgivemoreinfo',function(done){
我有以下风格的函数(在Node.JS下使用bluebirdpromises):module.exports={somefunc:Promise.method(functionsomefunc(v){if(v.data===undefined)thrownewError("Expectingdata");v.process_data="xxx";returnmodule.exports.someother1(v).then(module.exports.someother2).then(module.exports.someother3).then(module.exports.some
我正在尝试让gulp工作以帮助自动化一些单元测试。我有以下gulp文件。vargulp=require('gulp'),mocha=require('gulp-mocha');gulp.task('unit',function(){returngulp.src('test/unit/**/*.js').pipe(mocha({reporter:'spec'})).on('error',handleError);});gulp.task('watch',function(){gulp.watch(['src/**/*.js','test/unit/**/*.js'],['unit']);
有没有办法在express中测试这些中间件:module.exports=functionlogMatchingUrls(pattern){returnfunction(req,res,next){if(pattern.test(req.url)){console.log('requesturl',req.url);req.didSomething=true;}next();}}我发现的唯一中间件测试是:module.exports=function(request,response,next){/**DosomethingtoREQUESTorRESPONSE**/if(!reque
我想实时代理在其他环境中运行的测试结果。下面是一些我想要实现的伪代码:vartest=proxy.getCurrentTest();//=>{slow:200,timeout:2000,duration:235,result:'error'};vartmpIt=it('test1',function(){this.slow(test.slow);this.timeout(test.timeout);});tmpIt.close({duration:test.duration,result:test.result});//thisshouldmakethistestredintheout
我正试图找出在所有测试运行后将删除数据库和关闭连接的函数放在哪里。这是我的嵌套测试://db.connection.db.dropDatabase();//db.connection.close();describe('User',function(){beforeEach(function(done){});after(function(done){});describe('#save()',function(){beforeEach(function(done){});it('shouldhaveusernameproperty',function(done){user.save(