Node.js的摩卡异步测试不从回调返回 [英] Node.js Mocha async test doesn't return from callbacks
问题描述
我对如何包装在摩卡测试嵌套的异步回调心乱如麻。这是有问题的code样品:它叫亚马逊S3,检查文件是否存在:
var目录应该=要求(应当);
VAR的AppConfig =要求(../ config.js);
变种FS =要求(FS);
VAR异步=需要('异步');
VAR S3 =需要('aws2js')负载(S3,appConfig.awsAccessKeyId,appConfig.awsSecretAccessKey)。
s3.setBucket(appConfig.awsBucketName);
VAR test_user_id = 590170571;
VAR user_file_ code = test_user_id * 2;
变种output_file_template = ['wordcloud_r','polarity_r','emot_cat_r'];
描述(应显示上传文件到Amazon S3',函数(){
it.only(应上传三个地方的图形文件到Amazon S3桶',函数(完成){
async.each(output_file_template,功能(test_file里面,CB){
执行console.log(test_file里面);
s3.head(报告/ dsfsdf.dff',功能(ERR,RES){
如果(ERR){
执行console.log(ERR)
}
执行console.log(RES)
CB(空);
//进行(); //没了
});
//进行(); //没了
});
//进行(); //没了
});
});
无论是code挂起等待完成(如果我做了省略()) - 或者,在code完成后不回调,或节点抱怨说,做起来难()被调用多次
通过下面的帮助下,我的排序得到了它的工作,但它看起来像异步巫术炖
it.only(应上传三个地方的图形文件到Amazon S3桶',函数(完成){
async.series([函数(回调){
async.each(output_file_template,功能(test_file里面,CB){
执行console.log(test_file里面);
s3.head(报告/ dsfsdf.dff',功能(ERR,RES){
如果(ERR){
执行console.log(ERR)
}
执行console.log(RES)
CB();
回电话();
});
});
},函数(回调){
完成();
回电话();
}]);
});
尝试使用async.serial。内的第一个条目,则使用async.each通过多个循环来运行。在第二个条目,把()完成。
I'm utterly confused on how to wrap nested async callbacks in a Mocha test. Here is the offending code sample: It's calling Amazon S3 to check that files exist:
var should = require('should');
var appConfig = require("../config.js");
var fs = require('fs');
var async = require('async');
var s3 = require('aws2js').load('s3', appConfig.awsAccessKeyId, appConfig.awsSecretAccessKey);
s3.setBucket(appConfig.awsBucketName);
var test_user_id = 590170571;
var user_file_code = test_user_id * 2;
var output_file_template = ['wordcloud_r', 'polarity_r', 'emot_cat_r'];
describe('Should show uploaded files to amazon s3', function () {
it.only('should upload three local graphics files to Amazon S3 bucket', function (done) {
async.each(output_file_template, function (test_file, cb) {
console.log(test_file);
s3.head('reports/dsfsdf.dff', function (err, res) {
if (err) {
console.log(err)
}
console.log(res)
cb(null);
// done(); //nope
});
// done(); //nope
});
// done(); //nope
});
});
Either code hangs waiting to complete (if I omit done() ) - or, the code completes without callbacks, or, node complains that done() was called multiple times.
With the help below, I sort of got it working, but it looks like asynchronous voodoo stew
it.only('should upload three local graphics files to Amazon S3 bucket', function (done) {
async.series([function (callback) {
async.each(output_file_template, function (test_file, cb) {
console.log(test_file);
s3.head('reports/dsfsdf.dff', function (err, res) {
if (err) {
console.log(err)
}
console.log(res)
cb();
callback();
});
});
}, function (callback) {
done();
callback();
}]);
});
Try using async.serial. Inside the first entry, use async.each to run through multiple loops. In the second entry, put done().
这篇关于Node.js的摩卡异步测试不从回调返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!