Foreach with Promise不等待方法结果

时间:2018-02-13 18:23:34

标签: javascript arrays jasmine protractor

我正在尝试迭代量角器测试生成的JSON文件。我将所有文件名拉入一个数组并调用一个打开并解析每个文件的方法,将结果发布到数据库并传回一个传递/失败的标志。

我在这里尝试了所有的例子 you can tell Rstudio about the proxy by adding this lines to your Renviron file.并且仍会得到相同的结果。

实际调用该方法,但结果不会发布到db。我已经在单个文件上测试了parser.parseResults并且它成功发布到数据库,因此它必须与未正确解析的承诺有关。

在jasmine / protractor框架中是不是可以做这样的事情?或者代码中有什么问题吗?

我已经包含了我最新尝试的代码。 谢谢 恭



protractorParser.prototype.parseResults = function (fileName) {
    return new Promise((resolve, reject) => {

        console.log('In parseresults', fileName);
        json.readFile(fileName, function (err, obj) {
            try {

                if (err != null) {
                    console.log('error reading file',err);
                    reject(err);
                }
                console.log('obj - ',obj);
                var results = [];

                var Passed = 0;
                var Message = '';
                var Stack = '';
                for (var suite in obj) {
                    var specs = obj[suite].specs;
                    console.log('spec - ', specs);
                    if (specs.length > 0) {
                        for (var i = 0; i < specs.length; i++) {
                            var assert = specs[i];
                            var tcR = new RegExp(/TC[\d]+/);
                            var tc = assert.description.match(tcR);

                            if (!assert.failedExpectations.length) {
                                Passed = 1;
                            }
                            else {
                                assert.failedExpectations.forEach((expectation) => {
                                    Message = expectation.message;
                                    Stack = expectation.stack.split('\n')[1].trim();
                                })
                                Passed = 0;
                            }
                            if (tc != null) {
                                utility.TestDataManager.insertAutomationResults(tc[0], assert.description, Passed, process.env.testBuild,
                                    'P', Message, Stack, 0, moment().utcOffset(config.get('settings.timeOffset')).format('YYYY-MM-DDTHH:mm:ss'), '')
                                    .then(function (resp) {
                                        resolve(Passed);

                                    }, (err) => {
                                        console.log('Posting to Database failed ', err);
                                        reject(err);
                                    });
                            } else {
                                console.log('no test case found for test: ' + assert.description + ' -- skipping');
                                reject(err); 
                            }
                        }
                    }
                }
            }
            catch (err) {
                console.log('rejecting opening file');
                reject(err);
            }
        });
    })
}
&#13;
&#13;
&#13;

parser.parseResults代码

&#13;
&#13;
{{1}}
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:1)

如果obj中没有一个套件,只有一个规格,那么您的承诺要么根本不能解决,要么多次解决。

避免在protractorParser.prototype.parseResults = function (fileName) { return new Promise((resolve, reject) => { console.log('In parseresults', fileName); json.readFile(fileName, function (err, obj) { if (err != null) { console.log('error reading file', err); reject(err); } else { resolve(obj); } }); }).then(function(obj) { console.log('obj - ',obj); var results = []; for (var suite in obj) { var specs = obj[suite].specs; console.log('spec - ', specs); for (let i = 0; i < specs.length; i++) { const assert = specs[i]; const tcR = /TC[\d]+/; const tc = assert.description.match(tcR); let Passed = 1; let Message = ''; let Stack = ''; if (assert.failedExpectations.length) { const expectation = assert.failedExpectations[assert.failedExpectations.length-1]; Passed = 0; Message = expectation.message; Stack = expectation.stack.split('\n')[1].trim(); } if (tc != null) { const time = moment().utcOffset(config.get('settings.timeOffset')).format('YYYY-MM-DDTHH:mm:ss'); const promise = utility.TestDataManager.insertAutomationResults(tc[0], assert.description, Passed, process.env.testBuild, 'P', Message, Stack, 0, time, ''); results.push(promise.catch(err => { console.log('Posting to Database failed ', err); throw err; })); } else { console.log('no test case found for test: ' + assert.description + ' -- skipping'); // I don't think you want to `throw err` here, right? } } } return Promise.all(results); }); }; 构造函数中包含太多内容 - 始终在尽可能小的级别上进行promisify,然后使用promise chaining。

v-if