当我使用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(
我似乎无法调试mocha脚本。我可以像node--inspectscript.js这样使用检查器运行Node。然后这给了我一个url去调试,比如chrome-devtools://devtools/remote/...但是,当我将mocha与这一行mocha--inspecttest.js一起使用时,我无法调试。它说“调试器正在监听[::]:5858”。有什么方法可以让我使用Node的检查器调试mocha测试?转到localhost:5858会给我这个信息:Type:connectV8-Version:5.1.281.84Protocol-Version:1Embedding-Host
我想测试以下函数是否按预期执行:functionthrowNextTick(error){process.nextTick(function(){throwerror;});}这是我的尝试:describe("throwNextTick",function(){it("worksasexpected",function(next){varerror=newError("boo!");varrecordedError=null;process.once("uncaughtException",function(error){recordedError=error;});throwNext