在Mocha测试脚本中,try / catch块正在吞咽错误

时间:2017-12-01 10:54:45

标签: javascript try-catch mocha

我在Mocha脚本中面临try / catch的问题。虽然有些测试失败了,但是Mocha显示为"所有测试都通过"。下面是我的测试脚本文件,帮助我解决这个问题。在此代码中,我将数组中的输入字符串发布到URL并期望XML响应。我正在使用Xml2js模块来解析对JSON字符串的Xml响应并将XML子元素与expect进行比较

var expect = require('chai').expect;
var request = require('supertest');
var xml2js = require('xml2js');
var PrettyError = require('pretty-error');
var pe = new PrettyError();


var async = require('async');
var fs = require('fs');
var parser = new xml2js.Parser();
var build = require('./config').Build;

var build_url = build.Environment[build.Type].HostUrl;

    var inputArray1 = [];

    describe('Verifying Incoming post output response', function() {
     var index = 0;
     validateFields(index);

     function validateFields(index) {
      var input = inputArray1[index];

      var actualInputArray = input.split(",");

      describe(actualInputArray[0], function() {
       it(actualInputArray[1], function(done) {
        request(build_url).post('/lead')
         .send(actualInputArray[2])
         .end(function(err, res) {
          var resxml = res.text;
          console.log("resxml", resxml);
          parser.parseString(resxml, function(err, result) {
           try {
            expect(result['Header']['RESULT']).to.deep.equal([actualInputArray[3]]);
            expect(result['Header']['STATUS_CODE']).to.deep.equal([actualInputArray[4]]);
            expect(result['Header']['MESSAGE']).to.deep.equal( [actualInputArray[5]]); 

           } catch (err) {
            console.log(pe.render(err.message));

           }
           index = index + 1;
           if (index < inputArray1.length) {
            validateFields(index);
           }

           done();
          });
         });
       }).timeout(5000);
      });
     }
    });

1 个答案:

答案 0 :(得分:0)

当你发现错误时,仍会调用done()方法并解析promise,所以你的所有测试都会通过。

尝试将for(Map<String, Object> map : Documents){ map.put("Access", true); } 添加到catch中,并在发生错误时让测试失败。